0

My Zend\Form includes a Zend\Form\Element\Select with the attribute 'multiple' => 'multiple' and a NotEmpty validator with a custom isEmpty error message.
With the multiple attribute set, when I submit the form without selecting any options, I get the default "Value is required..." error message rather than my own.
When I remove the multiple attribute, I get the desired behavior, i.e., my custom error message.

So, what I am doing wrong?

Here is the quick-and-dirty test, with everything stuffed into my controller action for the sake of demonstration:

public function testAction() {

    $form = new \Zend\Form\Form;
    $factory = new \Zend\InputFilter\Factory;
    $form->add(
        [
            'name' => 'select',
            'type' => 'Zend\Form\Element\Select',
            'attributes' => [
                'multiple' => 'multiple',
            ],
            'options' =>[
                'value_options' => ['' => '', 1 => "one", 2 => "two", 3 => "three"],
            ] 
        ]

    );
    $filter = $factory->createInputFilter([
        'select' => [
            'name' => 'select',
            'required' => true,
            'filters' => [
                ['name' => 'StringTrim',],
            ],
            'validators' => [[
                'name' => 'NotEmpty',
                'options' => ['messages' => ['isEmpty'=> 'all Dharmas are forms of Emptiness']],
            ]],
        ]
    ]);
    $form->setInputFilter($filter);
    $form->add(['name'=> 'submit','type'=> 'submit', 'attributes'=> ['value'=> 'submit']]);
    $form->setAttribute('action','/my-project/index/test')
    $view = new ViewModel();
    $view->setTemplate('my-project/index/test')
        ->setVariables(['form' => $form]);
    if ($this->getRequest()->isPost()) {
        $data = $this->params()->fromPost();
        $form->setData($data);
        print_r($data); // just making sure, for sanity's sake
        if ($form->isValid()) {
            echo "valid!";
        } else {
            echo "validation failed.";
        }
    }
    return $view;
}

Just for the record, here is the view, although I know (from dumping the error messages) that the issue is not here:

<?php 
$form = $this->form;
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formCollection($form);
echo $this->form()->closeTag();

Thanks!

Wilt
  • 41,477
  • 12
  • 152
  • 203
David
  • 815
  • 8
  • 18
  • I just tryed to run your code (removing the `setAttribute` method call, which was causing a 500) and I can see the correct error message... – marcosh Jan 14 '16 at 18:37
  • really?! Fascinating. I was just coming back here to add that I am using ZF 2.5.1. And you? btw I will update the code to correct the ->setAttribute() which I meant to call on the $form, not the $view – David Jan 14 '16 at 20:34
  • I am using version 2.5.1 too... very weird... – marcosh Jan 14 '16 at 22:28

1 Answers1

0

It probably has to do with the fact that setting the multiple field will cause the Select class to change the validator.

You can see that here on line 227 in the getValidator method of the Zend\Form\Element\Select class.

$validator = new ExplodeValidator([
    'validator'      => $validator,
    'valueDelimiter' => null, // skip explode if only one value
]);

The ExplodeValidator will validate each value independently with the validator that you provided and this probably leads to the unexpected "Value is required..." error message. Most likely the validator will end up on line 186 setting $values as an array containing an empty string.

I think if you want to get your custom message is to set required => false and 'allow_empty' => false for your validator. In your case I think this should not be a problem...

Wilt
  • 41,477
  • 12
  • 152
  • 203
  • Well, setting `required => false` and `allow_empty => false` results in the input passing validation, as one would expect, so that doesn't do it. I will try to investigate what you're saying about ExplodeValidator. Thanks. – David Jan 19 '16 at 17:07
  • *"so that doesn't do it"* @David Why does it not do it? I don't get the problem. It will still validate and won't allow empty arrays for your multiple select list... – Wilt Jan 20 '16 at 11:04
  • No, I'm talking about overriding the default NotEmpty validator's error message with my own -- that's what I have not been able to do (other than with crude hacks in the view layer). – David Jan 20 '16 at 14:55