diff --git a/src/Symfony/Bundle/TwigBundle/Extension/FormExtension.php b/src/Symfony/Bundle/TwigBundle/Extension/FormExtension.php index 4f96e842111bf..4c4ab2a336521 100644 --- a/src/Symfony/Bundle/TwigBundle/Extension/FormExtension.php +++ b/src/Symfony/Bundle/TwigBundle/Extension/FormExtension.php @@ -181,7 +181,7 @@ public function renderLabel(FieldInterface $field, $label = null, array $paramet return $this->render($field, 'label', array( 'field' => $field, 'params' => $parameters, - 'label' => null !== $label ? $label : ucfirst(strtolower(str_replace('_', ' ', $field->getKey()))), + 'label' => null !== $label ? $label : $field->getLabel(), )); } diff --git a/src/Symfony/Component/Form/Field.php b/src/Symfony/Component/Form/Field.php index fbb3097c21c4a..a940b4b61f799 100644 --- a/src/Symfony/Component/Form/Field.php +++ b/src/Symfony/Component/Form/Field.php @@ -63,6 +63,7 @@ class Field extends Configurable implements FieldInterface public function __construct($key = null, array $options = array()) { + $this->addOption('label'); $this->addOption('data'); $this->addOption('trim', true); $this->addOption('required', true); @@ -71,7 +72,7 @@ public function __construct($key = null, array $options = array()) $this->addOption('value_transformer'); $this->addOption('normalization_transformer'); - $this->key = (string)$key; + $this->key = $key; if (isset($options['data'])) { // Populate the field with fixed data @@ -169,6 +170,14 @@ public function getName() return null === $this->parent ? $this->key : $this->parent->getName().'['.$this->key.']'; } + /** + * {@inheritDoc} + */ + public function getLabel() + { + return $this->getOption('label') ?: ucfirst(strtolower(str_replace('_', ' ', $this->key))); + } + /** * {@inheritDoc} */ diff --git a/src/Symfony/Component/Form/FieldInterface.php b/src/Symfony/Component/Form/FieldInterface.php index 205d0bd13a335..0df72e046f7ef 100644 --- a/src/Symfony/Component/Form/FieldInterface.php +++ b/src/Symfony/Component/Form/FieldInterface.php @@ -80,6 +80,13 @@ function getKey(); */ function getName(); + /** + * Returns the label of the field. + * + * @return string If no label is specified, the label is equal to its key. + */ + function getLabel(); + /** * Returns the ID of the field. *