0

I'm trying validate a multiple dropdownlist, like this:

example error

view/form:

<?=
$form->field($hours, 'hours_id', ['template' => '{label}{input}<span class="help-block">{hint}{error}</span>'])->dropDownList(Hours::getHierarchy(), ['size' => 10, 'multiple' => 'multiple'], ['prompt' => Yii::t('app', '-- Select --'),
])
?>

model rules:

['hours_id', 'each', 'rule' => ['integer']],

Result:

Hour is invalid.

Someone knows what's wrong? I tried to custom validation, and i get the same error.

Juan.Queiroz
  • 207
  • 1
  • 3
  • 13

2 Answers2

0

Change your model rule like this

 [['hours_id'], 'each','integer'],
Dharman
  • 30,962
  • 25
  • 85
  • 135
vichu
  • 353
  • 2
  • 10
0

By default multi select drop down for a field name will send options in following format:

name=value1&name=value2

To be able to receive data as array (as it suspected for each validation) You should use name[] instead of name.

For your case

<?=
    $form->field($hours, 'hours_id[]', ['template' => '{label}{input}<span class="help-block">{hint}{error}</span>'])->dropDownList(Hours::getHierarchy(), ['size' => 10, 'multiple' => 'multiple'], ['prompt' => Yii::t('app', '-- Select --')])
?>
oakymax
  • 1,454
  • 1
  • 14
  • 21