Allowempty In Type Array Validator

I created model rule for taking tabular input

array(‘table_ids’, ‘type’, ‘type’ => ‘array’, ‘allowEmpty’ => true) OR

array(‘table_ids’, ‘type’, ‘type’ => ‘array’, ‘allowEmpty’ => false)

And In view

echo $form->listBox($model, ‘table_ids’, $listdata, array(‘multiple’ => ‘true’));

Everything works ok except in the case that no item selected from the list

if allowEmpty=false no error validator occurs

both of cases (true or false) if no item checked the save fas no effect in the related items

The problem I think is that in POST submit the variable not exists so

the $model->attributes = $_POST[‘modelName’] not pass the Specific List

so the array of the model is no affected

Note: In afterFind method I fill the array table_ids from the database

How to manipulate this ?



Maybe you could add something like this before that rule:

    array('table_ids', 'default', 'value'=>array(), 'setOnEmpty'=>true),

Hi Keith thanks for the response

Validators works in setAttribute,

but afterFind I fill with data from database.

If on SetAttributes the post form contains an empty array "table_ids[]" I think everything works correctly

But in submit form the table_ids (when is empty) not exists! So validators check this attribute as exist from the database and not for the submitted form

I could check in my controller if isset($_POST[‘table_ids’]) -> $model->table_ids = array() but I want more robust way…

Any suggestion ?

Try this in combination with my first suggestion:

echo $form->listBox($model, 'table_ids', $listdata, array(

    'multiple' => 'true',

    'unselectValue' => '',


Thanks Keith with a vote

it works perfectly!


The first suggestion is not necessary, setAtributes will be set the attribute to empty string.

But if we want integration of our code without check empty($this->table_ids) or $this->table_ids==’’ then it is good practice to use it!