Prechádzať zdrojové kódy

added missing required classes on form label

Fabien Potencier 14 rokov pred
rodič
commit
9a6aafa83f

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

@@ -193,6 +193,9 @@
 
 {% block form_label %}
 {% spaceless %}
+    {% if required %}
+        {% set attr = attr|merge({'class': attr.class|default('') ~ ' required'}) %}
+    {% endif %}
     <label {% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>{{ label|trans }}</label>
 {% endspaceless %}
 {% endblock form_label %}

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

@@ -1 +1,2 @@
+<?php if ($required) { $attr['class'] = (isset($attr['class']) ? $attr['class'] : '').' required'; } ?>
 <label <?php foreach($attr as $k => $v) { printf('%s="%s" ', $view->escape($k), $view->escape($v)); } ?>><?php echo $view->escape($view['translator']->trans($label)) ?></label>

+ 17 - 0
tests/Symfony/Tests/Component/Form/AbstractLayoutTest.php

@@ -147,7 +147,24 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
 '
         );
     }
+/*
+    public function testLabelOnForm()
+    {
+        $form = $this->factory->createNamed('date', 'na&me', null, array(
+            'property_path' => 'name',
+        ));
+        $view = $form->createView();
+        $this->renderWidget($view, array('label' => 'foo'));
+        $html = $this->renderLabel($view);
 
+        $this->assertMatchesXpath($html,
+'/label
+    [@class=" required"]
+    [.="[trans]Na&me[/trans]"]
+'
+        );
+    }
+*/
     public function testLabelWithCustomTextPassedAsOption()
     {
         $form = $this->factory->createNamed('text', 'na&me', null, array(