In my scenario I have a form, where multiple fields for the same value are present. I have managed to validate the fields the folowing way in my controller:
foreach($_POST as $i=>$document){
if($i=='yt0') { continue;}
if($document!='not set'){
TDocumentToDelivery::model()->deleteAll('del_oid='.$id.' AND doc_oid=7 AND mst_oid='.$i);
$name=TMilestone::model()->findAllByAttributes(array('mst_oid'=>$i));
$name=$name[0]['mst_ticket'];
$attrib=array('doc_oid'=>7,'del_oid'=>$id,'doc_url'=>$document,'mst_oid'=>$i,'doc_version'=>$name);
$docs[$i]= new TDocumentToDelivery;
$docs[$i]->attributes=$attrib;
$docs[$i]->save();
$val=$val && $docs[$i]->validate();
}
}
if($val){
$model->del_status='testing';
$model->save();
$this->redirect(array('workflow','id'=>$model->del_oid));
}
}
In my view i used the solution i found on several forums:
The problem is, int the view, if a validation fails, each field is marked red, with the error message. How, should i write my form, that the error message is only connected to the field where the validation error actually occurs?
I looked at my $docmodel after the validation error, and the error is liked to the ‘doc_url’ field. There is no distinction between the different ‘doc_url[$i]’ fields. So the error message is written for each echo $form->error($docmodel,“doc_url[$i]”); command.
You are right, i pass the same model to each field. I should create a new model insatnce for each field. So if my foreach runs lets say 5 times i sould have 5 different models $docmodel1, $docmodel2 etc…
I am still strugling with the same problem, although it changed a bit. I Have to use htmlOptions in my textfield, and this seems to mess up the whole error handling.
The validation occurs, because if the field is not an url (my rule is array(‘doc_url’, ‘url’,‘allowEmpty’=>true),) the form is not submitted, but there is no error message. If i look at the $docmodel array, there is no sign of an error recorded either.