php - Cakephp 3 how to set maximum number of selected option in multiple select? -


i set rule limit number of option visitors can select in field multiple select.

i tried this, doesn't work

$validator     ->add('colors._ids', [         'multiple'=>[             'rule'=>['multiple', ['max'=>3]],             'message'=>'please select 1 color'         ]     ])     ->requirepresence('colors._ids', 'create'); //    ->allowempty('colors._ids');  return $validator; 

in view field displayed that:

echo $this->form->input('colors._ids', ['options' => $colors, 'multiple' => true]); 

using debugtoolkit can read:

  • _serialize(array)
  • car(array)
  • new(true)
  • accessible(array)
  • properties(array)
  • dirty(array)
  • original(empty)
  • virtual(empty)
  • errors(array)
  • colors._ids(array)
  • _required field required
  • repository cars
  • users(array)
  • colors(array)

thank help

i had similar problem , solve ended using custom validation rule.

so example:

->add('tablename', 'custom', [             'rule' => function($value) {                 return (bool)(is_array($value['_ids']) && count($value['_ids']) === 3);             },             'message' => 'please select 3.'         ]); 

this rule make sure user selects 3 items select. no more no less, i'm sure adapt problem.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -