Browse Source

[Form] Used ValuePlugin to pass displayed data to template. The order of added renderer plugins and set renderer variables is now respected

Bernhard Schussek 14 năm trước cách đây
mục cha
commit
68bb3ff606

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

@@ -43,7 +43,7 @@
 {% block widget %}
 {% spaceless %}
     {% set type = type|default('text') %}
-    <input type="{{ type }}" {{ block('attributes') }} value="{{ field.displayedData }}" />
+    <input type="{{ type }}" {{ block('attributes') }} value="{{ value }}" />
 {% endspaceless %}
 {% endblock widget %}
 
@@ -84,7 +84,7 @@
 
 {% block textarea__widget %}
 {% spaceless %}
-    <textarea {{ block('attributes') }}>{{ field.displayedData }}</textarea>
+    <textarea {{ block('attributes') }}>{{ value }}</textarea>
 {% endspaceless %}
 {% endblock textarea__widget %}
 
@@ -94,11 +94,11 @@
         {% if choice_list.isChoiceGroup(label) %}
             <optgroup label="{{ choice }}">
                 {% for nestedChoice, nestedLabel in label %}
-                    <option value="{{ nestedChoice }}"{% if choice_list.isChoiceSelected(nestedChoice, field.displayedData) %} selected="selected"{% endif %}>{{ nestedLabel }}</option>
+                    <option value="{{ nestedChoice }}"{% if choice_list.isChoiceSelected(nestedChoice, value) %} selected="selected"{% endif %}>{{ nestedLabel }}</option>
                 {% endfor %}
             </optgroup>
         {% else %}
-            <option value="{{ choice }}"{% if choice_list.isChoiceSelected(choice, field.displayedData) %} selected="selected"{% endif %}>{{ label }}</option>
+            <option value="{{ choice }}"{% if choice_list.isChoiceSelected(choice, value) %} selected="selected"{% endif %}>{{ label }}</option>
         {% endif %}
     {% endfor %}
 {% endspaceless %}

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

@@ -113,6 +113,7 @@ class FormFactory
             ->setRenderer(new DefaultRenderer($this->theme, $options['template']))
             ->addRendererPlugin(new IdPlugin($field))
             ->addRendererPlugin(new NamePlugin($field))
+            ->addRendererPlugin(new ValuePlugin($field))
             ->setRendererVar('field', $field)
             ->setRendererVar('class', null)
             ->setRendererVar('max_length', null)

+ 18 - 8
src/Symfony/Component/Form/Renderer/DefaultRenderer.php

@@ -25,7 +25,7 @@ class DefaultRenderer implements RendererInterface
 
     private $vars = array();
 
-    private $plugins = array();
+    private $changes = array();
 
     private $initialized = false;
 
@@ -35,13 +35,19 @@ class DefaultRenderer implements RendererInterface
         $this->template = $template;
     }
 
-    private function setUpPlugins()
+    private function initialize()
     {
         if (!$this->initialized) {
             $this->initialized = true;
 
-            foreach ($this->plugins as $plugin) {
-                $plugin->setUp($this);
+            // Make sure that plugins and set variables are applied in the
+            // order they were added
+            foreach ($this->changes as $key => $value) {
+                if ($value instanceof PluginInterface) {
+                    $value->setUp($this);
+                } else {
+                    $this->vars[$key] = $value;
+                }
             }
         }
     }
@@ -59,17 +65,21 @@ class DefaultRenderer implements RendererInterface
     public function addPlugin(PluginInterface $plugin)
     {
         $this->initialized = false;
-        $this->plugins[] = $plugin;
+        $this->changes[] = $plugin;
     }
 
     public function setVar($name, $value)
     {
-        $this->vars[$name] = $value;
+        if ($this->initialized) {
+            $this->vars[$name] = $value;
+        } else {
+            $this->changes[$name] = $value;
+        }
     }
 
     public function getVar($name)
     {
-        $this->setUpPlugins();
+        $this->initialize();
 
         // TODO exception handling
         return $this->vars[$name];
@@ -112,7 +122,7 @@ class DefaultRenderer implements RendererInterface
 
     protected function render($block, array $vars = array())
     {
-        $this->setUpPlugins();
+        $this->initialize();
 
         return $this->theme->render($this->template, $block, array_replace(
             $this->vars,