Search code examples
zend-framework2

ZF2 convert empty posted fields to Null


I'm using fieldsets in order to fill forms in ZF2. If an empty field is postedm, the field is also empty in the db. How do I force a Null in the db for empty fields?


Solution

  • In ZF2 I think you need to use Zend\Filter\Null or Zend\Filter\ToNull depending on which version of ZF2 you are using, Zend\Filter\Null became deprecated in ZF2.4.

    In your fieldset, assuming you are using the Zend\InputFilter\InputFilterProviderInterface use:

    public function getInputFilterSpecification()
    {
        return array(
            'your_field' => array(
                'filters' => array(
                    array('name' => 'ToNull'),
                ),
            ),
        );
    }