Browse Source

[Form] Added missing feature for adding attributes to an field label

stloyd 14 years ago
parent
commit
cb22ccc516

+ 7 - 2
src/Symfony/Bridge/Twig/Extension/FormExtension.php

@@ -151,10 +151,15 @@ class FormExtension extends \Twig_Extension
      *
      * @param FormView $view  The view to render the label for
      * @param string   $label Label name
+     * @param array    $variables Additional variables passed to the template
      */
-    public function renderLabel(FormView $view, $label = null)
+    public function renderLabel(FormView $view, $label = null, array $variables = array())
     {
-        return $this->render($view, 'label', null === $label ? array() : array('label' => $label));
+        if ($label !== null) {
+            $variables += array('label' => $label);
+        }
+
+        return $this->render($view, 'label', $variables);
     }
 
     protected function render(FormView $view, $section, array $variables = array())

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/field_label.html.php

@@ -1 +1 @@
-<label for="<?php echo $view->escape($id) ?>"><?php echo $view->escape($view['translator']->trans($label)) ?></label>
+<label for="<?php echo $view->escape($id) ?>" <?php echo $view['form']->attributes() ?>><?php echo $view->escape($view['translator']->trans($label)) ?></label>

+ 6 - 2
src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php

@@ -86,9 +86,13 @@ class FormHelper extends Helper
         return $this->renderSection($view, 'row', $variables);
     }
 
-    public function label(FormView $view, $label = null)
+    public function label(FormView $view, $label = null, array $variables = array())
     {
-        return $this->renderSection($view, 'label', null === $label ? array() : array('label' => $label));
+        if ($label !== null) {
+            $variables += array('label' => $label);
+        }
+
+        return $this->renderSection($view, 'label', $variables);
     }
 
     public function errors(FormView $view)

+ 1 - 1
src/Symfony/Bundle/TwigBundle/Resources/views/Form/div_layout.html.twig

@@ -37,7 +37,7 @@
 
 {% block field_label %}
 {% spaceless %}
-    <label for="{{ id }}">{{ label|trans }}</label>
+    <label for="{{ id }}"{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>{{ label|trans }}</label>
 {% endspaceless %}
 {% endblock field_label %}