How to replace a Form Element by a custom one in ZF2?

146 Views Asked by At

I'm struggling with the MultipleCheckbox and particularly with the problem, that the form validation fails, if no checkbox of the list is selected. (See details here.)

To workaround the issue I want to replace the Zend\Form\Element\Checkbox by a custom one where I override the getInputSpecification() method:

Checkbox.php

namespace My\Form\Element;
use Zend\Form\Element\Checkbox as ZendCheckbox;
class Checkbox extends ZendCheckbox
{
    public function getInputSpecification()
    {
        $spec = parent::getInputSpecification();
        $spec['required'] = false;
        return $spec;
    }
}

module.config.php

return [
    'form_elements' => [
        'invokables' => [
            'Zend\Form\Element\Checkbox' => 'My\Form\Element\Checkbox',
        ],
    ]
];

But my custom Checkbox class has not replaced the on of Zend. It's not ignored completely -- I see with Xdebug, that it's used in some cases (e.g. for a RadioButton element and for some single Checkbox elements). But in some other cases (particularly for the checkboxes of my MultiCheckbox) not.

What am I doing wrong? How to replace a Zend\Form\Element for the whole application?

0

There are 0 best solutions below