I have a form with two fields "a" and "b" when in action update if the "b" field changes the "beforeSubmit" event a Modal Bootstrap alert is sent to the user without any button OK or CANCEL, only information during 5 seconds, after this time automatically save if the the "b" field were actually changed, if not change save whitout alert modal windows.
How do I send this condition from the controller to view where I have the javascript? maybe with ajax? but how?
Controller.php
public function actionUpdate()
{
$model = new Faqs();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
if ($model->oldAttributes["b"] != $model->b){
sleep(5);
}
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
_form.php
$('#form').on('beforeSubmit', function(e) {
if(old_B_attribute != current_B_attribute){ //example
$('#modal').modal('show');
}
});
You want to prompt the user if the attribute values were actually changed before submitting the form.
How I would go for this
actionAttributeDirty()which would validate if the selected attribute was actually changed.Html::button()rather than aHtml::submitButton()for the form.idin the form.clickevent to the button which will send an ajax call toactionAttributeDirty()with theidof the current record.setTimeoutwith$.yiiActiveForm('submitForm')to trigger the form submission after 5 seconds.So in the similar order given above,
actionAttributeDirtyYour form should have the following buttons along with other fields,
clickEvent for the ButtonAdd the following on the top of your view where you have the form.
Note: change the
urlof the ajax call'/site/attribute-dirty'accordingly where you copy theactionAttributeDirty()i assume you copy it inside the site controller.EDIT
Pressing Enter button will not submit the form anyhow as there is no submit button, If you want Enter button to submit the form you should add the following along with the script too on the top of the view which will trigger the
clickevent of thesave-nowbutton whenever the Enter button is pressed within any input.