Forráskód Böngészése

[Form] Fixed: Checkboxes in an expanded choice field never have the 'required' HTML5 attribute. Closes http://trac.symfony-project.org/ticket/9588

Bernhard Schussek 14 éve
szülő
commit
30922d9375

+ 3 - 0
src/Symfony/Component/Form/Type/ChoiceType.php

@@ -44,6 +44,9 @@ class ChoiceType extends AbstractType
                     $builder->add((string)$choice, 'checkbox', array(
                         'value' => $choice,
                         'label' => $value,
+                        // The user can check 0 or more checkboxes. If required
+                        // is true, he is required to check all of them.
+                        'required' => false,
                     ));
                 } else {
                     $builder->add((string)$choice, 'radio', array(

+ 4 - 3
tests/Symfony/Tests/Component/Form/AbstractLayoutTest.php

@@ -412,16 +412,17 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
             'data' => array('&a', '&c'),
             'multiple' => true,
             'expanded' => true,
+            'required' => true,
         ));
 
         $this->assertWidgetMatchesXpath($form->createView(), array(),
 '/div
     [
-        ./input[@type="checkbox"][@name="na&me[&a]"][@id="na&me_&a"][@checked]
+        ./input[@type="checkbox"][@name="na&me[&a]"][@id="na&me_&a"][@checked][not(@required)]
         /following-sibling::label[@for="na&me_&a"][.="[trans]Choice&A[/trans]"]
-        /following-sibling::input[@type="checkbox"][@name="na&me[&b]"][@id="na&me_&b"][not(@checked)]
+        /following-sibling::input[@type="checkbox"][@name="na&me[&b]"][@id="na&me_&b"][not(@checked)][not(@required)]
         /following-sibling::label[@for="na&me_&b"][.="[trans]Choice&B[/trans]"]
-        /following-sibling::input[@type="checkbox"][@name="na&me[&c]"][@id="na&me_&c"][@checked]
+        /following-sibling::input[@type="checkbox"][@name="na&me[&c]"][@id="na&me_&c"][@checked][not(@required)]
         /following-sibling::label[@for="na&me_&c"][.="[trans]Choice&C[/trans]"]
     ]
     [count(./input)=3]

+ 42 - 0
tests/Symfony/Tests/Component/Form/Type/ChoiceTypeTest.php

@@ -56,6 +56,48 @@ class ChoiceTypeTest extends TestCase
         ));
     }
 
+    public function testExpandedCheckboxesAreNeverRequired()
+    {
+        $form = $this->factory->create('choice', 'name', array(
+            'multiple' => true,
+            'expanded' => true,
+            'required' => true,
+            'choices' => $this->choices,
+        ));
+
+        foreach ($form as $child) {
+            $this->assertFalse($child->isRequired());
+        }
+    }
+
+    public function testExpandedRadiosAreRequiredIfChoiceFieldIsRequired()
+    {
+        $form = $this->factory->create('choice', 'name', array(
+            'multiple' => false,
+            'expanded' => true,
+            'required' => true,
+            'choices' => $this->choices,
+        ));
+
+        foreach ($form as $child) {
+            $this->assertTrue($child->isRequired());
+        }
+    }
+
+    public function testExpandedRadiosAreNotRequiredIfChoiceFieldIsNotRequired()
+    {
+        $form = $this->factory->create('choice', 'name', array(
+            'multiple' => false,
+            'expanded' => true,
+            'required' => false,
+            'choices' => $this->choices,
+        ));
+
+        foreach ($form as $child) {
+            $this->assertFalse($child->isRequired());
+        }
+    }
+
     public function testBindSingleNonExpanded()
     {
         $form = $this->factory->create('choice', 'name', array(