public function actionPjax() {
$model = new \frontend\models\Product();
if ( $model->load ( Yii::$app->request->post () ) ) {
$model->validate();
if ( $model->hasErrors () ) {
$result = [];
// The code below comes from ActiveForm::validate(). We do not need to validate the model
// again, as it was already validated by save(). Just collect the messages.
foreach ( $model->getErrors () as $attribute => $errors ) {
$result[Html::getInputId ( $model , $attribute )] = $errors;
}
$errors=\yii\helpers\Json::encode([ 'validation' => $result ] );
$this->getView()->registerJs("var data='".$errors."';\$yiiform.yiiActiveForm('updateMessages', data.validation, true); // renders validation messages at appropriate places",\yii\web\View::POS_READY);
}
}
return $this->render ( 'pjax' , [
'model' => $model
] );
}
Now i want to update the error messages related to the form / model via javascript when i submit my form i call $model->validate() collect the messages into an array
Although there is a workaround to add $form->errorSummary($model) inside your view and just call validate and render without collecting the messages but what if the error is not related to model or I am throwing and catching an exception and I want to use sessionFlash to show this message it won't work with the session->setFlash too.
What steps will reproduce the problem?
Create a form within pjax
Add an action into your controller
Now i want to update the error messages related to the form / model via
javascript
when i submit my form i call$model->validate()
collect the messages into an arrayEncode them to
json
using$errors=\yii\helpers\Json::encode([ 'validation' => $result ] );
and then register a script from inside the actionWhat is the expected result?
Update messages to the form
What do you get instead?
Nothing happens even if you replace the statement
$this->getView()->registerJs("var data='".$errors."';\$yiiform.yiiActiveForm('updateMessages', data.validation, true);
with
$this->getView()->registerJs("alert('hello')");
Although there is a workaround to add
$form->errorSummary($model)
inside your view and just call validate and render without collecting the messages but what if the error is not related to model or I am throwing and catching an exception and I want to usesessionFlash
to show this message it won't work with thesession->setFlash
too.Am i doing it wrong altogether
Additional info