Validating A List

Hello,

pardon me asking the perhaps obvious.

So I have form with <select name=‘xyz’ multiple>

How to express and validate this in my model? Will the model receive an array or how this works?

Thank you

Hi,

I suppose the ‘xyz’ is a relation (HAS_MANY) with primary model?

Please post your model schema and the _form view

In general way you have to validate the multi related models each seperated.

It’s just a small test case. No db etc. Just a form with one select, which I want to submit and an action to display the submitted values.

So, for example, my select is:


 <select name="attrs" multiple>

   <option>1</option>

   <option>2</option>

   <option>3</option>

 </select>

But I cannot find out how to make the form model and how to validate the selected values.

in your formModel add this code




public $xyz=array();


public function rules()

{

   return array(

            array('xyz','type','type'=>'array','allowEmpty'=>false),

   );

}


public function beforeValidate() {

if(parent::beforeValidate()) {

foreach ($this->xyz as $val) {

if ($val ... not apply in your validation ) return

}

return true;

}

return false;

}

Great, Thank you!