zendframework / zend-validator

Validator component from Zend Framework
BSD 3-Clause "New" or "Revised" License
181 stars 136 forks source link

ZF3 Using Explode with InArray validator as child breaks validation messages format #230

Open dabroderick opened 6 years ago

dabroderick commented 6 years ago

I am using the following array notation in my form creation for a single select element:

'validators' => [
[
    'name' => 'Explode',
    'options' => [
        'validator' => [
            'name'=>'InArray', 
            'options'=>[
                'haystack'=>[1,2],
                'message'=> 'Invalid status'
            ]
        ]
    ]
],
]

When I submit an invalid option, I get this from $form->getMessages():

  array (size=3)
  'status' => 
    array (size=2)
      'notInArray' => string 'The input was not found in the haystack' (length=39)
      0 => 
        array (size=1)
          'notInArray' => string 'Invalid status' (length=14)

Without the Explode layer in the validators,

[
     'name'=>'InArray', 
     'options'=>[
          'haystack'=>[1,2],
          'message'=>'Invalid status'
     ]
],

$form->getMessages() reports this when invalid:

array (size=3)
  'status' => 
    array (size=1)
      'notInArray' => string 'Invalid status' (length=14)

Since Explode does not acknowledge a message option and when I set disable_inarray_validator to true I still get:

  'status' => 
    array (size=2)
      'notInArray' => string 'The input was not found in the haystack' (length=39)
      0 => 
        array (size=1)
          'notInArray' => string 'Invalid status' (length=14)

What I am expecting to get is:

'status' => 
    array (size=1)
      'notInArray' => 
        array (size=1)
          0 => string 'Invalid status' (length=14)

From what I can tell, Explode is a utility to help process validators rather than being an actual validator. But in using it, it breaks validation messages so my standard code for processing form validation messages does not work.

weierophinney commented 4 years ago

This repository has been closed and moved to laminas/laminas-validator; a new issue has been opened at https://github.com/laminas/laminas-validator/issues/5.