Pārlūkot izejas kodu

[Form] Add the ability to override label & widget options when rendering a row

Victor Berchet 14 gadi atpakaļ
vecāks
revīzija
f729c6ba93

+ 15 - 6
src/Symfony/Bridge/Twig/Extension/FormExtension.php

@@ -35,7 +35,7 @@ class FormExtension extends \Twig_Extension
     public function __construct(array $resources = array())
     {
         $this->themes = new \SplObjectStorage();
-        $this->varStack = new \SplObjectStorage();
+        $this->varStack = array();
         $this->blocks = new \SplObjectStorage();
         $this->rendering = array();
 
@@ -125,6 +125,15 @@ class FormExtension extends \Twig_Extension
      */
     public function renderRow(FormView $view, array $variables = array())
     {
+        $variables = array_replace_recursive(
+            array(
+                'label'     => array(),
+                'widget'    => array(),
+                'attr'      => array(),
+            ),
+            $variables
+        );
+        
         return $this->render($view, 'row', $variables);
     }
 
@@ -241,18 +250,18 @@ class FormExtension extends \Twig_Extension
 
                 $this->rendering[$rendering] = $i;
 
-                $this->varStack[$view] = array_replace(
-                    isset($this->varStack[$view]) ? $this->varStack[$view] : $view->all(),
+                $this->varStack[$rendering] = array_replace_recursive(
+                    isset($this->varStack[$rendering]) ? $this->varStack[$rendering] : $view->all(),
                     $variables
                 );
 
-                $html = $this->template->renderBlock($block, $this->varStack[$view], $blocks);
+                $html = $this->template->renderBlock($block, $this->varStack[$rendering], $blocks);
 
                 if ($mainTemplate) {
                     $view->setRendered();
                 }
 
-                unset($this->varStack[$view], $this->rendering[$rendering]);
+                unset($this->varStack[$rendering], $this->rendering[$rendering]);
 
                 return $html;
             }
@@ -270,7 +279,7 @@ class FormExtension extends \Twig_Extension
     {
         return 'form';
     }
-    
+
     /**
      * Returns the blocks used to render the view.
      *

+ 3 - 3
src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

@@ -188,10 +188,10 @@
 
 {% block field_row %}
 {% spaceless %}
-    <div>
-        {{ form_label(form) }}
+    <div {% for attrname,attrvalue in attr %}{{attrname}}="{{attrvalue}}" {% endfor %}>
+        {{ form_label(form, null, label) }}
         {{ form_errors(form) }}
-        {{ form_widget(form) }}
+        {{ form_widget(form, widget) }}
     </div>
 {% endspaceless %}
 {% endblock field_row %}

+ 3 - 3
src/Symfony/Bridge/Twig/Resources/views/Form/form_table_layout.html.twig

@@ -2,13 +2,13 @@
 
 {% block field_row %}
 {% spaceless %}
-    <tr>
+    <tr {% for attrname,attrvalue in attr %}{{attrname}}="{{attrvalue}}" {% endfor %}>
         <td>
-            {{ form_label(form) }}
+            {{ form_label(form, null, label) }}
         </td>
         <td>
             {{ form_errors(form) }}
-            {{ form_widget(form) }}
+            {{ form_widget(form, widget) }}
         </td>
     </tr>
 {% endspaceless %}

+ 3 - 3
src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_row.html.php

@@ -1,5 +1,5 @@
-<div>
-    <?php echo $view['form']->label($form) ?>
+<div <?php foreach($attr as $k => $v) { printf('%s="%s" ', $view->escape($k), $view->escape($v)); } ?>>
+    <?php echo $view['form']->label($form, null, $label) ?>
     <?php echo $view['form']->errors($form) ?>
-    <?php echo $view['form']->widget($form) ?>
+    <?php echo $view['form']->widget($form, $widget) ?>
 </div>

+ 15 - 8
src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php

@@ -38,8 +38,7 @@ class FormHelper extends Helper
     public function __construct(EngineInterface $engine)
     {
         $this->engine = $engine;
-        $this->varStack = new \SplObjectStorage();
-        $this->viewStack = array();
+        $this->varStack = array();
         $this->rendering = array();
         $this->context = array();
     }
@@ -103,6 +102,15 @@ class FormHelper extends Helper
      */
     public function row(FormView $view, array $variables = array())
     {
+        $variables = array_replace_recursive(
+            array(
+                'label'     => array(),
+                'widget'    => array(),
+                'attr'      => array()
+            ),
+            $variables
+        );
+        
         return $this->renderSection($view, 'row', $variables);
     }
 
@@ -191,18 +199,17 @@ class FormHelper extends Helper
 
                 $this->rendering[$rendering] = $i;
 
-                $this->varStack[$view] = array_replace(
-                    $view->all(),
-                    isset($this->varStack[$view]) ? $this->varStack[$view] : array(),
+                $this->varStack[$rendering] = array_replace_recursive(
+                    isset($this->varStack[$rendering]) ? $this->varStack[$rendering] : $view->all(),
                     $variables
                 );
 
-                $this->context[] = $this->varStack[$view];
+                $this->context[] = $this->varStack[$rendering];
 
-                $html = $this->engine->render($template, $this->varStack[$view]);
+                $html = $this->engine->render($template, $this->varStack[$rendering]);
 
                 array_pop($this->context);
-                unset($this->varStack[$view], $this->rendering[$rendering]);
+                unset($this->varStack[$rendering], $this->rendering[$rendering]);
 
                 if ($mainTemplate) {
                     $view->setRendered();

+ 10 - 5
tests/Symfony/Tests/Component/Form/AbstractDivLayoutTest.php

@@ -35,16 +35,21 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest
         );
     }
 
-    public function testRowForwardsVariables()
+    public function testRowOverrideVariables()
     {
         $view = $this->factory->createNamed('text', 'name')->createView();
-        $html = $this->renderRow($view, array('label' => 'foo&bar'));
+        $html = $this->renderRow($view, array(
+            'label'  => array('label' => 'foo&bar', 'attr' => array('class ' => 'label&class')),
+            'widget' => array('attr' => array('class' => 'widget&class')),
+            'attr'   => array('class' => 'row&class')
+
+        ));
 
         $this->assertMatchesXpath($html,
-'/div
+'/div[@class="row&class"]
     [
-        ./label[@for="name"][.="[trans]foo&bar[/trans]"]
-        /following-sibling::input[@id="name"]
+        ./label[@for="name"][.="[trans]foo&bar[/trans]"][@class="label&class"]
+        /following-sibling::input[@id="name"][@class="widget&class"]
     ]
 '
         );