Browse Source

[FrameworkBundle][Form] Fix label rendering

The label should not include the view 'id' attribute as it is used by the view widget.
Victor Berchet 14 năm trước cách đây
mục cha
commit
5044a7b56d

+ 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['form']->attributes() ?>><?php echo $view->escape($view['translator']->trans($label)) ?></label>
+<label for="<?php echo $view->escape($id) ?>" <?php echo $view['form']->attributes(false) ?>><?php echo $view->escape($view['translator']->trans($label)) ?></label>

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

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

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

@@ -38,7 +38,14 @@ class FormHelper extends Helper
         $this->varStack = new \SplObjectStorage();
     }
 
-    public function attributes()
+    /**
+     * Renders the attributes for the current view.
+     *
+     * @param Boolean $includeId Whether to render the id attribute
+     *
+     * @return string The HTML markup
+     */
+    public function attributes($includeId = true)
     {
         $html = '';
         $attr = array();
@@ -51,7 +58,7 @@ class FormHelper extends Helper
                 $attr = $vars['attr'];
             }
 
-            if (isset($vars['id'])) {
+            if (true === $includeId && isset($vars['id'])) {
                 $attr['id'] = $vars['id'];
             }
         }

+ 15 - 0
tests/Symfony/Tests/Component/Form/AbstractDivLayoutTest.php

@@ -386,6 +386,21 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest
             ]
     ]
     [count(//input)=2]
+'
+        );
+    }
+
+    public function testLabelHasNoId()
+    {
+        $form = $this->factory->createNamed('text', 'name');
+        $html = $this->renderRow($form->createView());
+
+        $this->assertMatchesXpath($html,
+'/div
+    [
+        ./label[@for="name"][not(@id)]
+        /following-sibling::input[@id="name"]
+    ]
 '
         );
     }