Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultiCheckbox: default input filter prevents submitting with no options checked #24

Open
weierophinney opened this issue Dec 31, 2019 · 2 comments

Comments

@weierophinney
Copy link
Member

See zendframework/zendframework#4694, zendframework/zendframework#2395, https://framework.zend.com/issues/browse/ZF2-571.

The issue remains the same now as it was in 2012: MultiCheckbox extends Checkbox. Checkbox's input filter defaults to required, a more or less sensible setup for an element that by default includes a hidden input so something should always be submitted. MultiCheckbox, sensibly, changes the default to not include the hidden input as it doesn't really make sense for a normal multi-checkbox setup. But, it doesn't override the input filter specification.

The result is that by default, a MultiCheckbox will throw a validation error when submitted with nothing checked.


Originally posted by @zerocrates at zendframework/zend-form#123

@weierophinney
Copy link
Member Author

Confirmed. Failing test case:

    public function testDefaultInputFilterAllowsSubmittingWithNoCheckedOptions()
    {
        $element = new MultiCheckboxElement();
        $element->setName('foo');
        $element->setValueOptions([
            'Option 1' => 'option1',
            'Option 2' => 'option2',
            'Option 3' => 'option3',
        ]);

        $form = new \Zend\Form\Form();
        $form->add($element);

        $form->setData([]);
        $this->assertTrue($form->isValid());
    }

Originally posted by @adamlundrigan at zendframework/zend-form#123 (comment)

@weierophinney
Copy link
Member Author

Adding this to MultiCheckboxElement will fix the issue:

    public function getInputSpecification()
    {
        $spec = parent::getInputSpecification();
        $spec['required'] = false;

        return $spec;
    }

However we're changing the default behaviour of the element (form will now validate successfully when the checkbox name is not present in the form data, whereas previously it would not). There are likely form implementations that rely on the default behaviour being to require at least one selection so this is a BC break IMO.

My suggestion is we consider this a documentation issue rather than a bug. The default behaviour is fine as long as it's known and expected, and overriding it is straightforward (pull the form element's corresponding Input from the InputFilter and call setRequired(false)). Perhaps we just add a section to the MultiCheckbox documentation page with that information?


Originally posted by @adamlundrigan at zendframework/zend-form#123 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants