Showing posts with label CActiveForm. Show all posts
Showing posts with label CActiveForm. Show all posts

Tuesday, November 9, 2010

Posting Multiple of the Same model within a form

Recently, I was asked about posting multiple models of the same class within a form and how that can be accomplished.

The solution is to override the default name values for the form items to allow them to be presented in an array format, then in the controller, process each array item for the Class.

Lets say that I have a set of user bookmarks (Bookmark), and I want them to be able to update all the bookmarks from a single administrative page. This is a little rough, but it should explain the concept clearly:

Wednesday, March 31, 2010

Posting with multiple models

I spent a good bit of time trying to figure out how to render multiple models in a form and have them each validate properly.

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