Laminas form - validate multicheckbox

Hello.
I have a problem with a form - with multicheckbox validation.

My code - sample definition multicheckbox input:

$this->add([
            'type' => Element\MultiCheckbox::class,
            'name' => self::ELEMENT_FRUIT,
            'options' => [
                'label' => 'Fruits',
                'value_options' => [
                    1 => 'Apple',
                    2 => 'Orange',
                    3 => 'Lemon',
                ],
            ],
        ]);

And validator:

[
                'name' => self::ELEMENT_FRUIT,
                'filters' => [
                    ['name' => ToInt::class],
                ],
                'validators' => [
                    [
                        'name' => NotEmpty::class,
                        'options' => [
                            'messages' => [
                                NotEmpty::IS_EMPTY => 'Choose fruit from the list.',
                            ],
                        ],
                    ],
                    [
                        'name' => InArray::class,
                        'options' => [
                            'haystack' => [1,2,3],
                            'messages' => [
                                InArray::NOT_IN_ARRAY => 'Unknown fruit. Choose again.',
                            ],
                        ],
                    ],
                ],
            ],

And now when I select one fruit for example, no matter which one, I get an error every time.

Could someone please help me?

Best regards

Which error?

Your example works like expected:

$form = (new Laminas\Form\Factory())->createForm(
    [
        'elements'     => [
            [
                'type'    => Laminas\Form\Element\MultiCheckbox::class,
                'name'    => 'fruits',
                'options' => [
                    'label'         => 'Fruits',
                    'value_options' => [
                        1 => 'Apple',
                        2 => 'Orange',
                        3 => 'Lemon',
                    ],
                ],
            ],
        ],
        'input_filter' => [
            [
                'name'       => 'fruits',
                'filters'    => [
                    ['name' => Laminas\Filter\ToInt::class],
                ],
                'validators' => [
                    [
                        'name'    => Laminas\Validator\NotEmpty::class,
                        'options' => [
                            'messages' => [
                                Laminas\Validator\NotEmpty::IS_EMPTY => 'Choose fruit from the list.',
                            ],
                        ],
                    ],
                    [
                        'name'    => Laminas\Validator\InArray::class,
                        'options' => [
                            'haystack' => [1, 2, 3],
                            'messages' => [
                                Laminas\Validator\InArray::NOT_IN_ARRAY => 'Unknown fruit. Choose again.',
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ]
);
$form->setData(['fruits' => '2']);
var_dump($form->isValid()); // true

$form->setData(['fruits' => '4']);
var_dump($form->isValid()); // false
var_dump($form->getMessages());
/*
 array(1) {
   ["fruits"]=>
   array(1) {
     ["notInArray"]=>
     string(28) "Unknown fruit. Choose again."
   }
 }
 */

I get an error every time (no matter if I select 1 or more fruits):
InArray::NOT_IN_ARRAY => ‘Unknown fruit. Choose again.’

var_dump($form->isValid());
bool(false)

var_dump($request->getPost());

object(Laminas\Stdlib\Parameters)#113 (1) {
  ["storage":"ArrayObject":private]=>
  array(3) {
    ["fruits"]=>
    array(1) {
      [0]=>
      string(1) "3"
    }
    ["fruits_csrf"]=>
    string(65) "572b8bea298a071c40fe783121dda3fb-48c7567d30d3b84cc8290521de64329b"
    ["submit"]=>
    string(4) "Save"
  }
}

var_dump($form->getData());

array(3) {
  ["fruits"]=>
  array(1) {
    [0]=>
    string(1) "3"
  }
  ["fruits_csrf"]=>
  string(65) "572b8bea298a071c40fe783121dda3fb-48c7567d30d3b84cc8290521de64329b"
  ["submit"]=>
  string(4) "Save"
}

Ah sorry, it is a “multicheckbox” element and therefore is your InArray validator wrong. You must use the Explode validator and set the InArray validator as validator:

[
                        'name' => Explode::class,
                        'options' => [
                            'validator' => [
                                'name' => InArray::class,
                                'options' => [
                                    'haystack' => [1, 2, 3],
                                ],
                            ],
                            'valueDelimiter' => ',',
                        ],
                    ],

Unfortunately, such a code does not work. How can I use Explode validator in my case?

Set the validator as instance:

[
    'name'    => Laminas\Validator\Explode::class,
    'options' => [
        'validator' => new Laminas\Validator\InArray(
            ['haystack' => [1, 2, 3]]
        ),
    ],
],

But you do not need to set the validator manually if the default is used or you do not need to set custom error message(s).

1 Like

Works perfectly!

@froschdesign Thank you very much for all your help :slight_smile: