Knowing the solution, it seems quite obvious, but it took a while to track down the proper syntax.
In this example I have a custom CForm which contains a few different $model properties, in addition to the base form data.
To call the validation on multiple models in the form, simply modify any calls of
CActiveForm::validate($model);to use an array of models as the first parameter as such:
CActiveForm::validate( array($model, $model->subModel, $model->subModel2) );
I don't know if you read this yet:
ReplyDeletehttp://www.yiiframework.com/doc/cookbook/19/
I did read that one and found it quite helpful! My confusion came in with the ajax validation and realizing that validate actually will convert a single element into an array for processing if you don't pass in an array.
ReplyDeleteThanks for your feedback btw =) Much appreciate the insights!
Have you ever tried to post multiple objects of the same model in a form? Something like
ReplyDeleteRendered view form:
CActiveForm {
$a = new Class1;
$b = new Class1;
...
echo $form->textField($a, 'field1');
...
echo $form->textField($b, 'field1');
...
}
I still couldnt find out a way to do it cause it seems the controller always takes only the last object of the class on the $_POST variable..
Yes, I have. You have to set it up as an array of $_POST['ObjectName'] items like so:
ReplyDelete(where $i is the current model count)
echo $form->textField($a, 'field1', array('name'=>"ObjectName[$i]['field1']"));
Then in your controller, treat the $_POST['ObjectName'] as an array and assign appropriately. Make sense?
To use your example:
ReplyDelete$form->textField($a, 'field1', array('name'=>'Class1[0][field1]));
$form->textField($b, 'field1', array('name'=>'Class1[1][field1]));
Then in the controller:
$a = new Class1;
$b = new Class1;
if ( isset($_POST['Class1']))
{
$a->attributes = $_POST['Class1'][0];
$b->attributes = $_POST['Class1'][1];
// continue processing here
}