Answer the question
In order to leave comments, you need to log in
Yii2 how to understand in the controller that the Save button is pressed in the form?
I raise the modal, render in it the part of the form responsible for the update.
I can’t figure out the appropriate behavior in the modal: when to validate, when to save, when to close the modal?
Ajax turned on, validation turned on and off, now it’s not about that, it seems to me.
Question: how can I check in the controller that the Save button was pressed in the form?
I think it would make things easier, something like this:
if (нажали save) {
if (валидация успешна) {
пишем данные, закрываем модалку
} else {
выводим результат валидации (рендерим аякс? или встроенная валидация?)
}
} else {
тут просто валидация, например
}
'enableAjaxValidation' => true,
Answer the question
In order to leave comments, you need to log in
1. The Save button has nothing to do with it.
2. The model has 2 methods $model->save() and $model->validate()
$model->save() - saves and validates
$model->validate() - only validates data.
Yours should be a little different:
if ($model->validate()) {
///здесь ваш код
$model->save()
пишем данные, закрываем модалку
} else {
выводим результат валидации (рендерим аякс? или встроенная валидация?)
}
}
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
///code
$model->save()
}
Like this
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
// you code
}
post request data is saved, so usually in the controller they write:
if ($request->isPost()) {}
well, or check the value transfer:
if ($request->post($buttonname)) {}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question