Skip to content

[TwigBridge] Provide a default non-empty label on empty options to validate HTML5 #48035

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

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
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 @@ -61,7 +61,7 @@
{%- endif -%}
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
{%- if placeholder is not none -%}
<option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ placeholder != '' ? (translation_domain is same as(false) ? placeholder : placeholder|trans({}, translation_domain)) }}</option>
<option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ placeholder != '' ? (translation_domain is same as(false) ? placeholder : placeholder|trans({}, translation_domain)) : '--' }}</option>
{%- endif -%}
{%- if preferred_choices|length > 0 -%}
{% set options = preferred_choices %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
{%- endif -%}
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple" data-customforms="disabled"{% endif %}>
{% if placeholder is not none -%}
<option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ translation_domain is same as(false) ? placeholder : placeholder|trans({}, translation_domain) }}</option>
<option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ translation_domain is same as(false) ? placeholder : placeholder|trans({}, translation_domain) : '--' }}</option>
{%- endif %}
{%- if preferred_choices|length > 0 -%}
{% set options = preferred_choices %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,8 @@ public function testSingleChoiceWithPreferredAndNoSeparator()
);
}

// Note: blank separator produces non standard HTML (option text should not be empty),
// but better not let Twig fix this automatically, user-side problem.
public function testSingleChoiceWithPreferredAndBlankSeparator()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', [
Expand Down Expand Up @@ -683,7 +685,7 @@ public function testSingleChoiceNonRequired()
[@class="my&class form-control"]
[not(@required)]
[
./option[@value=""][.=""]
./option[@value=""][.="" or .="--"]
/following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
]
Expand All @@ -707,7 +709,7 @@ public function testSingleChoiceNonRequiredNoneSelected()
[@class="my&class form-control"]
[not(@required)]
[
./option[@value=""][.=""]
./option[@value=""][.="" or .="--"]
/following-sibling::option[@value="&a"][not(@selected)][.="[trans]Choice&A[/trans]"]
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
]
Expand Down Expand Up @@ -766,6 +768,8 @@ public function testSingleChoiceRequiredWithPlaceholder()
);
}

// Note: blank placeholder produces non standard HTML (option text should not be empty),
// but better not let Twig fix this automatically, user-side problem.
public function testSingleChoiceRequiredWithPlaceholderViaView()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', [
Expand All @@ -781,7 +785,7 @@ public function testSingleChoiceRequiredWithPlaceholderViaView()
[@class="my&class form-control"]
[@required="required"]
[
./option[@value=""][not(@selected)][not(@disabled)][.=""]
./option[@value=""][not(@selected)][not(@disabled)][.="" or .="--"]
/following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
]
Expand Down Expand Up @@ -1935,6 +1939,8 @@ public function testBirthDay()
);
}

// Note: blank placeholder produces non standard HTML (option text should not be empty),
// but better not let Twig fix this automatically, user-side problem.
public function testBirthDayWithPlaceholder()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\BirthdayType', '1950-01-01', [
Expand All @@ -1950,17 +1956,17 @@ public function testBirthDayWithPlaceholder()
./select
[@id="name_month"]
[@class="form-control"]
[./option[@value=""][not(@selected)][not(@disabled)][.=""]]
[./option[@value=""][not(@selected)][not(@disabled)][.="" or .="--"]]
[./option[@value="1"][@selected="selected"]]
/following-sibling::select
[@id="name_day"]
[@class="form-control"]
[./option[@value=""][not(@selected)][not(@disabled)][.=""]]
[./option[@value=""][not(@selected)][not(@disabled)][.="" or .="--"]]
[./option[@value="1"][@selected="selected"]]
/following-sibling::select
[@id="name_year"]
[@class="form-control"]
[./option[@value=""][not(@selected)][not(@disabled)][.=""]]
[./option[@value=""][not(@selected)][not(@disabled)][.="" or .="--"]]
[./option[@value="1950"][@selected="selected"]]
]
[count(./select)=3]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,8 @@ public function testSingleChoiceWithPreferredAndNoSeparator()
);
}

// Note: blank separator produces non standard HTML (option text should not be empty),
// but better not let Twig fix this automatically, user-side problem.
public function testSingleChoiceWithPreferredAndBlankSeparator()
{
$form = $this->factory->createNamed('name', ChoiceType::class, '&a', [
Expand Down Expand Up @@ -667,7 +669,7 @@ public function testSingleChoiceNonRequired()
[@class="my&class form-select"]
[not(@required)]
[
./option[@value=""][.=""]
./option[@value=""][.="" or .="--"]
/following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
]
Expand All @@ -691,7 +693,7 @@ public function testSingleChoiceNonRequiredNoneSelected()
[@class="my&class form-select"]
[not(@required)]
[
./option[@value=""][.=""]
./option[@value=""][.="" or .="--"]
/following-sibling::option[@value="&a"][not(@selected)][.="[trans]Choice&A[/trans]"]
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
]
Expand Down Expand Up @@ -750,6 +752,8 @@ public function testSingleChoiceRequiredWithPlaceholder()
);
}

// Note: blank placeholder produces non standard HTML (option text should not be empty),
// but better not let Twig fix this automatically, user-side problem.
public function testSingleChoiceRequiredWithPlaceholderViaView()
{
$form = $this->factory->createNamed('name', ChoiceType::class, '&a', [
Expand All @@ -765,7 +769,7 @@ public function testSingleChoiceRequiredWithPlaceholderViaView()
[@class="my&class form-select"]
[@required="required"]
[
./option[@value=""][not(@selected)][not(@disabled)][.=""]
./option[@value=""][not(@selected)][not(@disabled)][.="" or .="--"]
/following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
]
Expand Down Expand Up @@ -1392,6 +1396,8 @@ public function testBirthDay()
);
}

// Note: blank placeholder produces non standard HTML (option text should not be empty),
// but better not let Twig fix this automatically, user-side problem.
public function testBirthDayWithPlaceholder()
{
$form = $this->factory->createNamed('name', BirthdayType::class, '1950-01-01', [
Expand All @@ -1410,17 +1416,17 @@ public function testBirthDayWithPlaceholder()
./select
[@id="name_month"]
[@class="form-select"]
[./option[@value=""][not(@selected)][not(@disabled)][.=""]]
[./option[@value=""][not(@selected)][not(@disabled)][.="" or .="--"]]
[./option[@value="1"][@selected="selected"]]
/following-sibling::select
[@id="name_day"]
[@class="form-select"]
[./option[@value=""][not(@selected)][not(@disabled)][.=""]]
[./option[@value=""][not(@selected)][not(@disabled)][.="" or .="--"]]
[./option[@value="1"][@selected="selected"]]
/following-sibling::select
[@id="name_year"]
[@class="form-select"]
[./option[@value=""][not(@selected)][not(@disabled)][.=""]]
[./option[@value=""][not(@selected)][not(@disabled)][.="" or .="--"]]
[./option[@value="1950"][@selected="selected"]]
]
[count(./select)=3]
Expand Down
18 changes: 12 additions & 6 deletions src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,8 @@ public function testSingleChoiceWithPreferredAndNoSeparator()
);
}

// Note: blank separator produces non standard HTML (option text should not be empty),
// but better not let Twig fix this automatically, user-side problem.
public function testSingleChoiceWithPreferredAndBlankSeparator()
{
$this->requiresFeatureSet(404);
Expand Down Expand Up @@ -841,7 +843,7 @@ public function testSingleChoiceNonRequired()
[@name="name"]
[not(@required)]
[
./option[@value=""][.=""]
./option[@value=""][.="" or .="--"]
/following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
]
Expand All @@ -864,7 +866,7 @@ public function testSingleChoiceNonRequiredNoneSelected()
[@name="name"]
[not(@required)]
[
./option[@value=""][.=""]
./option[@value=""][.="" or .="--"]
/following-sibling::option[@value="&a"][not(@selected)][.="[trans]Choice&A[/trans]"]
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
]
Expand Down Expand Up @@ -924,6 +926,8 @@ public function testSingleChoiceRequiredWithPlaceholder()
);
}

// Note: blank placeholder produces non standard HTML (option text should not be empty),
// but better not let Twig fix this automatically, user-side problem.
public function testSingleChoiceRequiredWithPlaceholderViaView()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', [
Expand All @@ -941,7 +945,7 @@ public function testSingleChoiceRequiredWithPlaceholderViaView()
[@name="name"]
[@required="required"]
[
./option[@value=""][not(@selected)][not(@disabled)][.=""]
./option[@value=""][not(@selected)][not(@disabled)][.="" or .="--"]
/following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
]
Expand Down Expand Up @@ -1687,6 +1691,8 @@ public function testBirthDay()
);
}

// Note: blank placeholder produces non standard HTML (option text should not be empty),
// but better not let Twig fix this automatically, user-side problem.
public function testBirthDayWithPlaceholder()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\BirthdayType', '1950-01-01', [
Expand All @@ -1700,15 +1706,15 @@ public function testBirthDayWithPlaceholder()
[
./select
[@id="name_month"]
[./option[@value=""][not(@selected)][not(@disabled)][.=""]]
[./option[@value=""][not(@selected)][not(@disabled)][.="" or .="--"]]
[./option[@value="1"][@selected="selected"]]
/following-sibling::select
[@id="name_day"]
[./option[@value=""][not(@selected)][not(@disabled)][.=""]]
[./option[@value=""][not(@selected)][not(@disabled)][.="" or .="--"]]
[./option[@value="1"][@selected="selected"]]
/following-sibling::select
[@id="name_year"]
[./option[@value=""][not(@selected)][not(@disabled)][.=""]]
[./option[@value=""][not(@selected)][not(@disabled)][.="" or .="--"]]
[./option[@value="1950"][@selected="selected"]]
]
[count(./select)=3]
Expand Down