Преглед изворни кода

[Form] Field labels can now be passed in the 'label' option

Bernhard Schussek пре 14 година
родитељ
комит
41c6ab0ac7

+ 8 - 1
src/Symfony/Component/Form/Type/FieldType.php

@@ -56,6 +56,7 @@ class FieldType extends AbstractType
             ->setAttribute('validation_groups', $options['validation_groups'])
             ->setAttribute('error_mapping', $options['error_mapping'])
             ->setAttribute('max_length', $options['max_length'])
+            ->setAttribute('label', $options['label'] ?: $this->humanize($builder->getName()))
             ->setData($options['data'])
             ->addValidator(new DefaultValidator())
             ->addValidator(new DelegatingValidator($this->validator));
@@ -87,7 +88,7 @@ class FieldType extends AbstractType
         $view->setVar('class', null);
         $view->setVar('max_length', $form->getAttribute('max_length'));
         $view->setVar('size', null);
-        $view->setVar('label', ucfirst(strtolower(str_replace('_', ' ', $form->getName()))));
+        $view->setVar('label', $form->getAttribute('label'));
         $view->setVar('multipart', false);
         $view->setVar('attr', array());
 
@@ -112,6 +113,7 @@ class FieldType extends AbstractType
             'validation_groups' => null,
             'error_bubbling' => false,
             'error_mapping' => array(),
+            'label' => null,
         );
 
         if (!empty($options['data_class'])) {
@@ -140,4 +142,9 @@ class FieldType extends AbstractType
     {
         return 'field';
     }
+
+    private function humanize($text)
+    {
+        return ucfirst(strtolower(str_replace('_', ' ', $text)));
+    }
 }

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

@@ -133,7 +133,23 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
         );
     }
 
-    public function testLabelWithCustomText()
+    public function testLabelWithCustomTextPassedAsOption()
+    {
+        $form = $this->factory->create('text', 'na&me', array(
+            'property_path' => 'name',
+            'label' => 'Custom label',
+        ));
+        $html = $this->renderLabel($form->createView());
+
+        $this->assertMatchesXpath($html,
+'/label
+    [@for="na&me"]
+    [.="[trans]Custom label[/trans]"]
+'
+        );
+    }
+
+    public function testLabelWithCustomTextPassedDirectly()
     {
         $form = $this->factory->create('text', 'na&me', array('property_path' => 'name'));
         $html = $this->renderLabel($form->createView(), 'Custom label');