8000 [Form] Fixed: "required" attribute is not added to <select> tag if no empty value by webmozart · Pull Request #9030 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Fixed: "required" attribute is not added to <select> tag if no empty value #9030

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

Merged
merged 1 commit into from
Sep 13, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@

{% block choice_widget_collapsed %}
{% spaceless %}
{% if required and empty_value is none and not empty_value_in_choices %}
{% set required = false %}
{% endif %}
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
{% if empty_value is not none %}
<option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ empty_value|trans({}, translation_domain) }}</option>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<select
<?php echo $view['form']->block($form, 'widget_attributes') ?>
<?php echo $view['form']->block($form, 'widget_attributes', array(
'required' => $required && (null !== $empty_value || $empty_value_in_choices)
)) ?>
<?php if ($multiple): ?> multiple="multiple"<?php endif ?>
>
<?php if (null !== $empty_value): ?><option value=""<?php if ($required and empty($value) && "0" !== $value): ?> selected="selected"<?php endif?>><?php echo $view->escape($view['translator']->trans($empty_value, array(), $translation_domain)) ?></option><?php endif; ?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ public function buildView(FormView $view, FormInterface $form, array $options)
}

// Check if the choices already contain the empty value
$view->vars['empty_value_in_choices'] = 0 !== count($options['choice_list']->getChoicesForValues(array('')));

// Only add the empty value option if this is not the case
if (null !== $options['empty_value'] && 0 === count($options['choice_list']->getChoicesForValues(array('')))) {
if (null !== $options['empty_value'] && !$view->vars['empty_value_in_choices']) {
$view->vars['empty_value'] = $options['empty_value'];
}

Expand Down
22 changes: 17 additions & 5 deletions src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,22 @@ public function testSingleChoice()
'expanded' => false,
));

// If the field is collapsed, has no "multiple" attribute, is required but
// has *no* empty value, the "required" must not be added, otherwise
// the resulting HTML is invalid.
// https://github.com/symfony/symfony/issues/8942

// HTML 5 spec
// http://www.w3.org/html/wg/drafts/html/master/forms.html#placeholder-label-option

// "If a select element has a required attribute specified, does not
// have a multiple attribute specified, and has a display size of 1,
// then the select element must have a placeholder label option."

$this->assertWidgetMatchesXpath($form->createView(), array(),
'/select
[@name="name"]
[@required="required"]
[not(@required)]
[
./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
Expand All @@ -394,7 +406,7 @@ public function testSingleChoiceWithPreferred()
$this->assertWidgetMatchesXpath($form->createView(), array('separator' => '-- sep --'),
'/select
[@name="name"]
[@required="required"]
[not(@required)]
[
./option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
/following-sibling::option[@disabled="disabled"][not(@selected)][.="-- sep --"]
Expand All @@ -417,7 +429,7 @@ public function testSingleChoiceWithPreferredAndNoSeparator()
$this->assertWidgetMatchesXpath($form->createView(), array('separator' => null),
'/select
[@name="name"]
[@required="required"]
[not(@required)]
[
./option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
/following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
Expand All @@ -439,7 +451,7 @@ public function testSingleChoiceWithPreferredAndBlankSeparator()
$this->assertWidgetMatchesXpath($form->createView(), array('separator' => ''),
'/select
[@name="name"]
[@required="required"]
[not(@required)]
[
./option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
/following-sibling::option[@disabled="disabled"][not(@selected)][.=""]
Expand Down Expand Up @@ -1704,7 +1716,7 @@ public function testTimezone()
$this->assertWidgetMatchesXpath($form->createView(), array(),
'/select
[@name="name"]
[@required="required"]
[not(@required)]
[./optgroup
[@label="[trans]Europe[/trans]"]
[./option[@value="Europe/Vienna"][@selected="selected"][.="[trans]Vienna[/trans]"]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,7 @@ public function testPassEmptyValueToView($multiple, $expanded, $required, $empty
$view = $form->createView();

$this->assertEquals($viewValue, $view->vars['empty_value']);
$this->assertFalse($view->vars['empty_value_in_choices']);
}

/**
Expand All @@ -1067,6 +1068,7 @@ public function testDontPassEmptyValueIfContainedInChoices($multiple, $expanded,
$view = $form->createView();

$this->assertNull($view->vars['empty_value']);
$this->assertTrue($view->vars['empty_value_in_choices']);
}

public function getOptionsWithEmptyValue()
Expand Down
0