Form\Element\Select multiple is always required #6009

Merged
merged 5 commits into from Apr 14, 2014

Conversation

Projects
None yet
4 participants
Contributor

tommyseus commented Mar 20, 2014

The Select Element is always required. The Validation fails (required) if the multiple select is unselected. It is not possible to send an empty list.

I added the option for an hidden element. If nothing is selected the empty hidden field is send (required).

Select Element has the new Option 'use_hidden_element' and 'unselected_value'.
The FormSelect Helper generates an hidden element before the select element.

Reference to other Issues.
#4750, #5842, doctrine/DoctrineModule#215

library/Zend/Form/Element/Select.php
@@ -270,4 +346,13 @@ protected function getOptionValue($key, $optionSpec)
{
return is_array($optionSpec) ? $optionSpec['value'] : $key;
}
+
+ /**
+ * @return boolean
@samsonasik

samsonasik Mar 20, 2014

Contributor

@return bool for consistency

@@ -235,6 +292,24 @@ public function getInputSpecification()
'required' => true,
);
+ if ($this->useHiddenElement() && $this->isMultiple()) {
+ $unselectedValue = $this->getUnselectedValue();
@Ocramius

Ocramius Apr 3, 2014

Member

What happens if $unselectedValue corresponds with a value in the dropdown?

@weierophinney

weierophinney Apr 14, 2014

Owner

@Ocramius It would either be marked as selected, or that value would be considered a default value.

Contributor

tommyseus commented Apr 3, 2014

If nothing is selected, the hidden value returns a string.
$select = 'dummy'
The filter removes the dummy value and $form->getData()['select'] returns an empty array.

If at least one value is selected, the select element sends an array.
$select = array('selected-option')

There is no conflict, because the results are different types.

I have added a testcase to the dataPovider for your example.

@weierophinney weierophinney added this to the 2.3.1 milestone Apr 14, 2014

@weierophinney weierophinney self-assigned this Apr 14, 2014

weierophinney added a commit that referenced this pull request Apr 14, 2014

Merge pull request #6009 from tommyseus/master
Form\Element\Select multiple is always required

weierophinney added a commit that referenced this pull request Apr 14, 2014

@weierophinney weierophinney merged commit 2849ed4 into zendframework:master Apr 14, 2014

1 check failed

continuous-integration/travis-ci The Travis CI build failed
Details

weierophinney added a commit that referenced this pull request Apr 14, 2014

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