1
votes

I'm building a form with input filters and all in Zend Framework 2. I'm using arrays to configure my inputs and filters and validators. So, say I have some input:

array(
    'name' => 'foo',
    'required' => true,
)

On the page, there's some jQuery code that might optionally hide this input. If it does, it hides and disables the input. On submit, var_dump on the form data gives foo => null (probably because it didn't actually submit in the post data and so the form input is never given a value). If the input is not hidden and not filled out then on submit it has foo => string '' (length=0).

I want to require the input to have a value if the input is used. So, are there some settings I can add to my config array that will allow null to pass validation, but reject the empty string value? Or do I need to write a custom validator?

1

1 Answers

3
votes

Yes, you ought to use NotEmpty validator.

$inputFilter = new \Zend\InputFilter\InputFilter ();
$inputFilter->add ([
  'name' => 'foo',
  'validators' => [
    [
      'name' => 'not_empty',
      'options' => [
        'type' => 'string'
      ]
    ]
  ]
]);

$form->setInputFilter ($inputFilter);