Validating model array attributes

I have a CFormModel that has attributes that are arrays. I was wondering how I might be able to handle validating these attributes? Validators are pretty strict about having attributes that aren’t arrays. I would like to use one single model, not many models. Is there a way around this WITHOUT using tabular data(multiple models)? And if so, how might I go about doing this?

Thanks in advance.

Is a bit strange that a CFormModel has array attributes.

CFormModel should be used for receive user input, how can a user input an array?

Maybe do you want to collect a tabular input?

Thanks for your reply.

I thought about using tabular data, but it doesn’t seem like an optimal solution in my case. I am building a more complex search page that has a dynamic number of attributes.

The page would have a url such as




  domain.com/controller/action?attrib1[0]=value0&attrib1[1]=value1&attrib1[2]=value2&attrib2[0]=value3



Each of the attrib params will be a set number of attributes within the model, but the number of values within that attribute could be unlimited (perhaps 40 models with 1 attribute, or 1 model with an attribute that is an array with 40 values). The issue is that I need to validate the data and then after validation, I need direct access to all data at one time. For example




  CFormModel->attrib1= array('value0', 'value1', 'value2');

  CFormModel->attrib2= array('value3');



will be a lot more convenient in my case than




  CFormModel1->attrib1 = 'value0';

  CFormModel1->attrib2 = 'value3';

  CFormModel2->attrib1 = 'value1';

  CFormModel3->attrib1 = 'value2';



I even thought about doing it like this:




  CFormModel->attrib1 = 'value0|value1|value2';

  CFormModel->attrib2 = 'value3';



Any other suggestions? My biggest concerns at this point are validation and all data being accessible at once.

I guess that you are in the situation of tabular input.

Create a simple CFormModel with attrib1 and attrib2, and then you will manage an array of such model.

You can validate this attribute in CFormModel. Is a simple change of implementation: an array of CFormModel instead of an array of attribute.

Yii has instrument for manage arrays of model, so use this instrument.

Your idea is good too, but Yii works differently. Follow the idea of the framework, and everthing will be simple

Attributes can be arrays. Think about select boxes that allow multiple selection or checkbox lists. This has nothing to do with tabular input. Instead i’d suggest to write your own validator class (or method). See here for an example how to write such a custom validator method:

http://www.yiiframework.com/doc/guide/form.model#declaring-validation-rules

I had a similar situation therefore I created this extension: array-validator