validate multiple model instances


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);








				$docs[$i]= new TDocumentToDelivery;



				$val=$val && $docs[$i]->validate();	

















In my view i used the solution i found on several forums:

echo $form->textField($docmodel,"doc_url[$i]");

echo $form->error($docmodel,"doc_url[$i]"); 

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?

You can use the error like that:

echo $form->error($docmodel,"doc_url[$i]"); 

Anyway, it should work correctly with the code you posted.

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.

The error is take from the actual instance of the model.

If you are ciclying correctly the model (and so at each step you have a different instance in $docmodel ) it should work.

The error is not in the function error, maybe you are simply passing the same model (or all model has the error)

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…

foreach($milestones as $i=>$mst){

	$doc[$i]=new TDocumentToDelivery;


		echo "<div class='row'>";

		echo $form->textField($doc[$i],"doc_url[$i]");

		echo $form->error($doc[$i],"doc_url[$i]");


		echo "</div>";


So i modified my code like this. I see no erros at all now! What did i do wrong?


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.


		echo $form->textField($docmodel,'doc_url',array('name'=>$mst['mst_oid'],'size'=>30,'maxlength'=>1024,'value'=>$url));

		echo $form->error($docmodel,'doc_url'); 


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.

Please help!

try this:


It is an empty array.