Copy The Validation Of One Field To Another

I have several fields with validation on the client and need to copy the validations to other fields that are created via javascript.

Is this possible? As would be done?

I am not sure mate, if there is a ready made solution in Yii. You would need to write your own javascript for this and build upon Yii’s client validation. Not sure about any third party plugin which directly does this.

You can add the fields to your model, eg.




    public $new_var;



Then add them to your model rules.

@Backslider, Note that I am talking about the same field being replicated dynamically via javascript, in a tabular form.

Athos if its tabular data you need to validate, you can simply use the following naming convention and yii will automatically do it for you…




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

   echo '<tr><td>' . $form->field($model, "[$i]attribute") . '</td></tr>';

}



Note the ‘attribute’ above will be validated automatically by yii for all rows based on the model validation rule, if you can render the form like above within your yii activeform.

But if you are using javascript to render the above… you may need to think of the approach on calling the Yii client validator. The server validation should work anyway. You can use ajax call to render the data as well and thus reinitialize the client validation… so depends on what approach you take.