From 49e0f50d71ebb99889c4c40afd88232bb94bb9cf Mon Sep 17 00:00:00 2001 From: "William Pinaud (DocFX)" Date: Fri, 28 Oct 2022 22:17:39 +0200 Subject: [PATCH] [TwigBridge] Provide a default non-empty label on empty options to validate HTML5 --- .../views/Form/form_div_layout.html.twig | 2 +- .../views/Form/foundation_5_layout.html.twig | 2 +- .../Extension/AbstractBootstrap3LayoutTest.php | 18 ++++++++++++------ .../Extension/AbstractBootstrap5LayoutTest.php | 18 ++++++++++++------ .../Form/Tests/AbstractLayoutTest.php | 18 ++++++++++++------ 5 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig b/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig index 61f64e33fa751..2e331f7064533 100644 --- a/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig +++ b/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig @@ -61,7 +61,7 @@ {%- endif -%} {% if placeholder is not none -%} - + {%- endif %} {%- if preferred_choices|length > 0 -%} {% set options = preferred_choices %} diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap3LayoutTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap3LayoutTest.php index 808352300adf4..e5aae4445bdd5 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap3LayoutTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap3LayoutTest.php @@ -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', [ @@ -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]"] ] @@ -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]"] ] @@ -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', [ @@ -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]"] ] @@ -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', [ @@ -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] diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap5LayoutTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap5LayoutTest.php index 7ac695ec927c3..b1686b1496730 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap5LayoutTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap5LayoutTest.php @@ -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', [ @@ -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]"] ] @@ -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]"] ] @@ -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', [ @@ -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]"] ] @@ -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', [ @@ -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] diff --git a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php index 58e853e111e5a..d2278b771e96a 100644 --- a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php @@ -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); @@ -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]"] ] @@ -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]"] ] @@ -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', [ @@ -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]"] ] @@ -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', [ @@ -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]