Hi everyone,
After taking much time for searching, I have a solution to ajax validation for tabular or dynamic input. It works for me. Hopefully it is helpful for someone. It is not a perfect but it gives you the concept how to implement this.
First,remember to enable ajaxValidation in the CActiveForm.
Next, Gennerate the inputs in the view, for example:
[VIEW]
In the contoller, rewrite the validate function which is copied and a bit modified from CActiveForm
Finally, in the controller, modify ajax perform function:
That is all how to enable ajax validation for tabular or dynamic input.
Hope help someone.
After taking much time for searching, I have a solution to ajax validation for tabular or dynamic input. It works for me. Hopefully it is helpful for someone. It is not a perfect but it gives you the concept how to implement this.
First,remember to enable ajaxValidation in the CActiveForm.
Next, Gennerate the inputs in the view, for example:
[VIEW]
<div class="row"> <?php for($i=1;$i<=5;$i++) { $m = new AgentPostcode('reg'); //your model ?> <?php echo $form->textField($m,"[$i]postcode"); ?> <?php echo $form->error($m,"[$i]postcode"); ?> <?php } ?> </div>
In the contoller, rewrite the validate function which is copied and a bit modified from CActiveForm
public static function validate($models, $dynamicModel, $attributes=null, $loadInput=true) { //copy from CActiveForm:validate $result=array(); if(!is_array($models)) $models=array($models); foreach($models as $model) { if($loadInput && isset($_POST[get_class($model)])) $model->attributes=$_POST[get_class($model)]; $model->validate($attributes); foreach($model->getErrors() as $attribute=>$errors) $result[CHtml::activeId($model,$attribute)]=$errors; } //end copying //your own customization for($count=1;$count<=5;$count++) { $dynamicModel->clearErrors(); if($loadInput && isset($_POST[get_class($dynamicModel)][$count])) $dynamicModel->attributes=$_POST[get_class($dynamicModel)][$count]; $dynamicModel->validate($attributes); foreach($dynamicModel->getErrors() as $attribute=>$errors) $result[get_class($dynamicModel) . '_' . $count . '_' . $attribute]=$errors; } return function_exists('json_encode') ? json_encode($result) : CJSON::encode($result); }
Finally, in the controller, modify ajax perform function:
// For Ajax Validation if(isset($_POST['ajax']) && $_POST['ajax']==='agent-registeragent-form') { $dynamicModel = new AgentPostcode('reg'); //your model $validate = self::validate(array($model,$modelAC),$dynamicModel); //$validate = CActiveForm::validate(array($model,$modelAC)); //original echo $validate; Yii::app()->end(); }
That is all how to enable ajax validation for tabular or dynamic input.
Hope help someone.