Bladeren bron

[Form] made required part of the algorithm to determine if an empty value should be added to a choice

Fabien Potencier 14 jaren geleden
bovenliggende
commit
6de97c56e1

+ 17 - 2
src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php

@@ -65,13 +65,28 @@ class ChoiceType extends AbstractType
             }
         }
 
+        // empty value
+        if ($options['multiple'] || $options['expanded']) {
+            // never use and empty value for these cases
+            $emptyValue = null;
+        } elseif (false === $options['empty_value']) {
+            // an empty value should be added but the user decided otherwise
+            $options['empty_value'] = null;
+        } elseif (null === $options['empty_value']) {
+            // user did not made a decision, so we put a blank empty value
+            $emptyValue = $options['required'] ? null : '';
+        } else {
+            // empty value has been set explicitely
+            $emptyValue = $options['empty_value'];
+        }
+
         $builder
             ->setAttribute('choice_list', $options['choice_list'])
             ->setAttribute('preferred_choices', $options['preferred_choices'])
             ->setAttribute('multiple', $options['multiple'])
             ->setAttribute('expanded', $options['expanded'])
             ->setAttribute('required', $options['required'])
-            ->setAttribute('empty_value', $options['multiple'] || $options['expanded'] ? null : $options['empty_value'])
+            ->setAttribute('empty_value', $emptyValue)
         ;
 
         if ($options['expanded']) {
@@ -133,7 +148,7 @@ class ChoiceType extends AbstractType
             'choices'           => array(),
             'preferred_choices' => array(),
             'empty_data'        => $multiple || $expanded ? array() : '',
-            'empty_value'       => ($multiple || $expanded) || !isset($options['empty_value']) ? null : '',
+            'empty_value'       => $multiple || $expanded || !isset($options['empty_value']) ? null : '',
             'error_bubbling'    => false,
         );
     }

+ 2 - 0
src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php

@@ -33,6 +33,7 @@ class DateTimeType extends AbstractType
             'months',
             'days',
             'empty_value',
+            'required',
         )));
         $timeOptions = array_intersect_key($options, array_flip(array(
             'hours',
@@ -40,6 +41,7 @@ class DateTimeType extends AbstractType
             'seconds',
             'with_seconds',
             'empty_value',
+            'required',
         )));
 
         if (isset($options['date_widget'])) {

+ 3 - 0
src/Symfony/Component/Form/Extension/Core/Type/DateType.php

@@ -55,18 +55,21 @@ class DateType extends AbstractType
                         array_combine($options['years'], $options['years']), 4, '0', STR_PAD_LEFT
                     ),
                     'empty_value' => $options['empty_value']['year'],
+                    'required' => $options['required'],
                 );
                 $monthOptions = array(
                     'choice_list' => new MonthChoiceList(
                         $formatter, $options['months']
                     ),
                     'empty_value' => $options['empty_value']['month'],
+                    'required' => $options['required'],
                 );
                 $dayOptions = array(
                     'choice_list' => new PaddedChoiceList(
                         array_combine($options['days'], $options['days']), 2, '0', STR_PAD_LEFT
                     ),
                     'empty_value' => $options['empty_value']['day'],
+                    'required' => $options['required'],
                 );
             }
 

+ 6 - 3
src/Symfony/Component/Form/Extension/Core/Type/TimeType.php

@@ -40,13 +40,15 @@ class TimeType extends AbstractType
                     'choice_list' => new PaddedChoiceList(
                         array_combine($options['hours'], $options['hours']), 2, '0', STR_PAD_LEFT
                     ),
-                    'empty_value' => $options['empty_value']['hour']
+                    'empty_value' => $options['empty_value']['hour'],
+                    'required' => $options['required'],
                 ))
                 ->add('minute', $options['widget'], array(
                     'choice_list' => new PaddedChoiceList(
                         array_combine($options['minutes'], $options['minutes']), 2, '0', STR_PAD_LEFT
                     ),
-                    'empty_value' => $options['empty_value']['minute']
+                    'empty_value' => $options['empty_value']['minute'],
+                    'required' => $options['required'],
                 ))
             ;
 
@@ -55,7 +57,8 @@ class TimeType extends AbstractType
                     'choice_list' => new PaddedChoiceList(
                         array_combine($options['seconds'], $options['seconds']), 2, '0', STR_PAD_LEFT
                     ),
-                    'empty_value' => $options['empty_value']['second']
+                    'empty_value' => $options['empty_value']['second'],
+                    'required' => $options['required'],
                 ));
             }
         }

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

@@ -381,10 +381,11 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
     [@name="na&me"]
     [not(@required)]
     [
-        ./option[@value="&a"][@selected="selected"][.="Choice&A"]
+        ./option[@value=""][.="[trans][/trans]"]
+        /following-sibling::option[@value="&a"][@selected="selected"][.="Choice&A"]
         /following-sibling::option[@value="&b"][not(@selected)][.="Choice&B"]
     ]
-    [count(./option)=2]
+    [count(./option)=3]
 '
         );
     }
@@ -404,10 +405,11 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
     [@name="na&me"]
     [not(@required)]
     [
-        ./option[@value="&a"][not(@selected)][.="Choice&A"]
+        ./option[@value=""][.="[trans][/trans]"]
+        /following-sibling::option[@value="&a"][not(@selected)][.="Choice&A"]
         /following-sibling::option[@value="&b"][not(@selected)][.="Choice&B"]
     ]
-    [count(./option)=2]
+    [count(./option)=3]
 '
         );
     }