Zend2 InputFilter: not required as default setting

183 Views Asked by At

I wrote an input filter for a Form by inheriting from InputFilter (class CustomInputFilter extends Zend\InputFilter\InputFilter).

By default, each form element is required. This means that I have to set required to False explicitly for all optional elements. How can I change the InputFilter such that all form elements are optional by default?

I use the Zend 2 framework.

1

There are 1 best solutions below

0
unclexo On

I am not sure about it is possible the way you want using InputFilter. I think this implies filtering and validating data. If any input field does not need to be required, then ZF have had the option ('required' => false). See another approach

$inputFilter->add(array(
    'name'        => 'title',
    'required'    => true,
    'allow_empty' => true,
));

So why is this? This is something that input field must exist but you can keep it empty. If you then put invalid data in it can then validate data too.