浏览代码

[Form] Refactored PercentField to FormFactory

Bernhard Schussek 14 年之前
父节点
当前提交
c8275c5b27

+ 1 - 0
src/Symfony/Bundle/TwigBundle/Resources/views/form.html.twig

@@ -198,6 +198,7 @@
 
 {% block percent__widget %}
 {% spaceless %}
+    {% set type = type|default('text') %}
     {{ block('widget') }} %
 {% endspaceless %}
 {% endblock percent__widget %}

+ 16 - 0
src/Symfony/Component/Form/FormFactory.php

@@ -38,6 +38,7 @@ use Symfony\Component\Form\ValueTransformer\BooleanToStringTransformer;
 use Symfony\Component\Form\ValueTransformer\NumberToLocalizedStringTransformer;
 use Symfony\Component\Form\ValueTransformer\IntegerToLocalizedStringTransformer;
 use Symfony\Component\Form\ValueTransformer\MoneyToLocalizedStringTransformer;
+use Symfony\Component\Form\ValueTransformer\PercentToLocalizedStringTransformer;
 use Symfony\Component\Form\ValueTransformer\ScalarToChoicesTransformer;
 use Symfony\Component\Form\ValueTransformer\DateTimeToArrayTransformer;
 use Symfony\Component\Form\ValueTransformer\DateTimeToStringTransformer;
@@ -268,6 +269,21 @@ class FormFactory
             ->addRendererPlugin(new MoneyPatternPlugin($options['currency']));
     }
 
+    public function getPercentField($key, array $options = array())
+    {
+        $options = array_merge(array(
+            'template' => 'percent',
+            'precision' => 0,
+            'type' => 'fractional',
+        ), $options);
+
+        return $this->getField($key, $options)
+            ->setValueTransformer(new PercentToLocalizedStringTransformer(array(
+                'precision' => $options['precision'],
+                'type' => $options['type'],
+            )));
+    }
+
     public function getCheckboxField($key, array $options = array())
     {
         $options = array_merge(array(

+ 0 - 54
src/Symfony/Component/Form/PercentField.php

@@ -1,54 +0,0 @@
-<?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;
-
-use Symfony\Component\Form\ValueTransformer\PercentToLocalizedStringTransformer;
-
-/**
- * A localized field for entering percentage values.
- *
- * The percentage is always rendered in its large format (e.g. 75, not .75).
- *
- * Available options:
- *
- *  * percent_type:     How the source number is stored on the object
- *                       * self::FRACTIONAL (e.g. stored as .75)
- *                       * self::INTEGER (e.g. stored as 75)
- *
- * By default, the precision option is set to 0, meaning that decimal integer
- * values will be rounded using the method specified in the rounding-mode
- * option.
- *
- * @see Symfony\Component\Form\NumberField
- * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
- */
-class PercentField extends NumberField
-{
-    const FRACTIONAL = 'fractional';
-    const INTEGER = 'integer';
-
-    /**
-     * {@inheritDoc}
-     */
-    protected function configure()
-    {
-        $this->addOption('precision', 0);
-        $this->addOption('percent_type', self::FRACTIONAL);
-
-        parent::configure();
-
-        $this->setValueTransformer(new PercentToLocalizedStringTransformer(array(
-            'precision' => $this->getOption('precision'),
-            'type' => $this->getOption('percent_type'),
-        )));
-    }
-}