Kaynağa Gözat

[Form] Moved submission logic of ChoiceField to preprocessor

Bernhard Schussek 14 yıl önce
ebeveyn
işleme
6edff6b8a9

+ 2 - 15
src/Symfony/Component/Form/ChoiceField.php

@@ -14,6 +14,7 @@ namespace Symfony\Component\Form;
 use Symfony\Component\Form\ChoiceList\DefaultChoiceList;
 use Symfony\Component\Form\ValueTransformer\ArrayToChoicesTransformer;
 use Symfony\Component\Form\ValueTransformer\ScalarToChoicesTransformer;
+use Symfony\Component\Form\DataProcessor\RadioToArrayConverter;
 
 /**
  * Lets the user select between different choices.
@@ -85,6 +86,7 @@ class ChoiceField extends HybridField
                 $this->setValueTransformer(new ArrayToChoicesTransformer($this->choiceList));
             } else {
                 $this->setValueTransformer(new ScalarToChoicesTransformer($this->choiceList));
+                $this->setDataPreprocessor(new RadioToArrayConverter());
             }
         }
     }
@@ -123,19 +125,4 @@ class ChoiceField extends HybridField
             ));
         }
     }
-
-    /**
-     * {@inheritDoc}
-     *
-     * Takes care of converting the input from a single radio button
-     * to an array.
-     */
-    public function submit($value)
-    {
-        if (!$this->isMultipleChoice() && $this->isExpanded()) {
-            $value = null === $value ? array() : array($value => true);
-        }
-
-        parent::submit($value);
-    }
 }

+ 28 - 0
src/Symfony/Component/Form/DataProcessor/RadioToArrayConverter.php

@@ -0,0 +1,28 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Form\DataProcessor;
+
+use Symfony\Component\Form\FieldInterface;
+
+/**
+ * Takes care of converting the input from a single radio button
+ * to an array.
+ *
+ * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
+ */
+class RadioToArrayConverter implements DataProcessorInterface
+{
+    public function processData($data)
+    {
+        return count((array)$data) === 0 ? array() : array($data => true);
+    }
+}

+ 6 - 6
src/Symfony/Component/Form/Field.php

@@ -415,7 +415,7 @@ class Field extends Configurable implements FieldInterface
      *
      * @param ValueTransformerInterface $valueTransformer
      */
-    protected function setNormalizationTransformer(ValueTransformerInterface $normalizationTransformer)
+    public function setNormalizationTransformer(ValueTransformerInterface $normalizationTransformer)
     {
         $this->normalizationTransformer = $normalizationTransformer;
 
@@ -427,7 +427,7 @@ class Field extends Configurable implements FieldInterface
      *
      * @return ValueTransformerInterface
      */
-    protected function getNormalizationTransformer()
+    public function getNormalizationTransformer()
     {
         return $this->normalizationTransformer;
     }
@@ -437,7 +437,7 @@ class Field extends Configurable implements FieldInterface
      *
      * @param ValueTransformerInterface $valueTransformer
      */
-    protected function setValueTransformer(ValueTransformerInterface $valueTransformer)
+    public function setValueTransformer(ValueTransformerInterface $valueTransformer)
     {
         $this->valueTransformer = $valueTransformer;
 
@@ -449,7 +449,7 @@ class Field extends Configurable implements FieldInterface
      *
      * @return ValueTransformerInterface
      */
-    protected function getValueTransformer()
+    public function getValueTransformer()
     {
         return $this->valueTransformer;
     }
@@ -459,7 +459,7 @@ class Field extends Configurable implements FieldInterface
      *
      * @param DataProcessorInterface $dataProcessor
      */
-    protected function setDataProcessor(DataProcessorInterface $dataProcessor)
+    public function setDataProcessor(DataProcessorInterface $dataProcessor)
     {
         $this->dataProcessor = $dataProcessor;
 
@@ -471,7 +471,7 @@ class Field extends Configurable implements FieldInterface
      *
      * @return DataProcessorInterface
      */
-    protected function getDataProcessor()
+    public function getDataProcessor()
     {
         return $this->dataProcessor;
     }

+ 32 - 3
src/Symfony/Component/Form/Form.php

@@ -22,6 +22,7 @@ use Symfony\Component\Form\Exception\UnexpectedTypeException;
 use Symfony\Component\Form\Exception\DanglingFieldException;
 use Symfony\Component\Form\Exception\FieldDefinitionException;
 use Symfony\Component\Form\CsrfProvider\CsrfProviderInterface;
+use Symfony\Component\Form\DataProcessor\DataProcessorInterface;
 
 /**
  * Form represents a form.
@@ -70,6 +71,8 @@ class Form extends Field implements \IteratorAggregate, FormInterface
      */
     protected $context = null;
 
+    private $dataPreprocessor;
+
     /**
      * Creates a new form with the options stored in the given context
      *
@@ -408,12 +411,12 @@ class Form extends Field implements \IteratorAggregate, FormInterface
             $data = array();
         }
 
-        // remember for later
-        $submittedData = $data;
-
         // might return an array, if $data isn't one already
         $data = $this->preprocessData($data);
 
+        // remember for later
+        $submittedData = $data;
+
         if (!is_array($data)) {
             throw new UnexpectedTypeException($data, 'array');
         }
@@ -495,6 +498,10 @@ class Form extends Field implements \IteratorAggregate, FormInterface
      */
     protected function preprocessData($data)
     {
+        if ($this->dataPreprocessor) {
+            return $this->dataPreprocessor->processData($data);
+        }
+
         return $data;
     }
 
@@ -958,6 +965,28 @@ class Form extends Field implements \IteratorAggregate, FormInterface
         return true;
     }
 
+    /**
+     * Sets the data preprocessor
+     *
+     * @param DataProcessorInterface $dataPreprocessor
+     */
+    public function setDataPreprocessor(DataProcessorInterface $dataPreprocessor)
+    {
+        $this->dataPreprocessor = $dataPreprocessor;
+
+        return $this;
+    }
+
+    /**
+     * Returns the data preprocessor
+     *
+     * @return DataPreprocessorInterface
+     */
+    public function getDataPreprocessor()
+    {
+        return $this->dataPreprocessor;
+    }
+
     /**
      * Merges two arrays without reindexing numeric keys.
      *