Yii2 Form Multiple Items Validation

Actually I am sending a lot of "Models" from a Form using arrays.

When I create the arrays in the controller and I pass them to the view, Yii automatically loads the javascript needed for the validation of the model, but if I create with javascript and other field, the last one does not hve any validation.

Any way to add the validation to the field that I am already adding with javascript ?

In the last picture you can see the validation working in the first row, the other rows were added using javascript.

So how to add the validation to the new rows that contain the model data ?

Use an array naming convention for your attributes and Yii will automatically do the validation for you (if you use Model::validateMultiple method). For example:




foreach ($models as $i=>$model) {

    echo $form->field($model, "[$i]attribute");

}



You can also refer a wiki that I created for this for Yii2.

The trouble is that if I send the info from the controller, yii creates automatically the validation, but if I create the fields after ( using javascript ) I can’t load these validators.

So could be cool to know how to call them after I already load the website.

you can use whenClient option for validators.

Which one is that option ? How can I use it ?

whenClient is this property of yii\validators\Validator.

You can try getting your validation using same custom code, and use whenClient to validate client side.

How are you creating these fields with Javascript?

IMO… you could render a view containing yii ActiveForm fields - through Javascript using ajax… which will call the ActiveForm validations.

This is probably the best answer. Yii has no knowledge of what client side cloning you are doing via query… Whereas if you have an Ajax function that spits out a model instance you get all the usual behavior.

This could be what I am looking for, but no idea on how to do it :P. To much to ask if you have an example ?

Will probably create a wiki for this when I have time (as I personally do not have a ready code for this). Just trying to share some thoughts on doing this… see if this helps. You basically create a view for rendering each table row… with active fields - the view can have these as parameters

  • the Model instance

  • the table row index number

  • the Active form instance (how you will store it across the session is for you to figure out)

based on this return a Json encoded renderPartial content of this view from controller action using a ajax call.

Hello i’m new for yii2 framework and i have the same question. I create form where i can add some related models via js. For this time i don’t find example how correctly create form elements via js.

When page loaded all related model displayed and saved correctly via php. Problems when i start using js. Please help me or show some example.

Hola CTala lograste resolverlo???, tengo el mismo problema.

La verdad lo tengo en StandBy, volveré pronto a retomar. Avísame como te va.

En mi caso solo era que el formulario no se cerraba bien por que estaban mal acomodados los div, puse el formulario hasta por encima de todo en la vista.

Hello Kartik, first thank you so much for your great work on Yii2 widgets, components etc that you have created. They helped us alot in developing yii2 applications.

However, this topic in this thread is a major issue and i think yii team need to look into it. I can remember vividly that in Yii1, it took me almost 3 days to develop custom code that could dynamically generate array of input fields with Jquery and validate these array of fields through my controller.

Yii2 is here again, we still have thesame problem. Though there seem to be a section in yii2 doc that talks about "loadMultiple" and validate multiple http://www.yiiframework.com/doc-2.0/guide-input-tabular-input.html.(this doc is for update array of models through input fields) but there is not doc on how create multiple input fields and validate them out of the box without any stress. I have been trying to do this taks for the past 2 days with on complete solution. Alot of yii users are having same issues out there.

I propose that YII team should include ability to dynamically and implicitly create array of form elements/fields through yii Jquery and model validation rules that works out of the box.

Kindly help look into this.