Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

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

Open
zerocrates opened this issue Sep 28, 2016 · 3 comments

Comments

@zerocrates
Copy link

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.

@adamlundrigan
Copy link
Contributor

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());
    }

@adamlundrigan
Copy link
Contributor

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?

@weierophinney
Copy link
Member

This repository has been closed and moved to laminas/laminas-form; a new issue has been opened at laminas/laminas-form#24.

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

No branches or pull requests

3 participants