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

Form\Element\Select multiple is always required #6009

Merged
merged 5 commits into from
Apr 14, 2014
Merged

Form\Element\Select multiple is always required #6009

merged 5 commits into from
Apr 14, 2014

Conversation

tommyseus
Copy link
Contributor

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

@@ -270,4 +346,13 @@ protected function getOptionValue($key, $optionSpec)
{
return is_array($optionSpec) ? $optionSpec['value'] : $key;
}

/**
* @return boolean
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@return bool for consistency

@@ -235,6 +292,24 @@ public function getInputSpecification()
'required' => true,
);

if ($this->useHiddenElement() && $this->isMultiple()) {
$unselectedValue = $this->getUnselectedValue();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@tommyseus
Copy link
Contributor Author

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
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
weierophinney added a commit that referenced this pull request Apr 14, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants