Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Form] allowed an empty value to be displayed for choices even when r…
…equired is true

Rules are as follows:

* If multiple is true, then the empty_value is ignored
* If not, and if the field is not required, the empty_value is set to the empty string by default and displayed
* If the field is required, and if the user explicitely set the empty_value, then it is displayed
  • Loading branch information
fabpot committed Jun 14, 2011
1 parent b29597a commit c364008
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
Expand Up @@ -133,7 +133,7 @@
</div>
{% else %}
<select {{ block('attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
{% if not multiple and not required %}
{% if not multiple and empty_value is not none %}
<option value="">{{ empty_value }}</option>
{% endif %}
{% if preferred_choices|length > 0 %}
Expand Down
Expand Up @@ -12,7 +12,7 @@
<?php if ($read_only): ?> disabled="disabled"<?php endif ?>
<?php if ($multiple): ?> multiple="multiple"<?php endif ?>
>
<?php if (!$multiple && !$required): ?><option value=""><?php echo $empty_value; ?></option><?php endif; ?>
<?php if (!$multiple && null !== $empty_value): ?><option value=""><?php echo $empty_value; ?></option><?php endif; ?>
<?php if (count($preferred_choices) > 0): ?>
<?php foreach ($preferred_choices as $choice => $label): ?>
<?php if ($view['form']->isChoiceGroup($label)): ?>
Expand Down
Expand Up @@ -70,6 +70,7 @@ public function buildForm(FormBuilder $builder, array $options)
->setAttribute('preferred_choices', $options['preferred_choices'])
->setAttribute('multiple', $options['multiple'])
->setAttribute('expanded', $options['expanded'])
->setAttribute('required', $options['required'])
;

if ($options['expanded']) {
Expand Down Expand Up @@ -105,7 +106,7 @@ public function buildView(FormView $view, FormInterface $form)
->set('preferred_choices', array_intersect_key($choices, $preferred))
->set('choices', array_diff_key($choices, $preferred))
->set('separator', '-------------------')
->set('empty_value', '')
->set('empty_value', !$form->getAttribute('multiple') && !$form->getAttribute('required') ? '' : null)
;

if ($view->get('multiple') && !$view->get('expanded')) {
Expand Down
23 changes: 23 additions & 0 deletions tests/Symfony/Tests/Component/Form/AbstractLayoutTest.php
Expand Up @@ -380,6 +380,29 @@ public function testSingleChoiceNonRequired()
);
}

public function testSingleChoiceRequiredWithEmptyValue()
{
$form = $this->factory->createNamed('choice', 'na&me', '&a', array(
'property_path' => 'name',
'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
'required' => true,
'multiple' => false,
'expanded' => false,
));

$this->assertWidgetMatchesXpath($form->createView(), array('empty_value' => ''),
'/select
[@name="na&me"]
[
./option[@value=""][.=""]
/following-sibling::option[@value="&a"][@selected="selected"][.="Choice&A"]
/following-sibling::option[@value="&b"][not(@selected)][.="Choice&B"]
]
[count(./option)=3]
'
);
}

public function testSingleChoiceGrouped()
{
$form = $this->factory->createNamed('choice', 'na&me', '&a', array(
Expand Down

0 comments on commit c364008

Please sign in to comment.