From 36c648bb192071e69452301edba410df5638fde8 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 21 Feb 2011 19:08:24 +0100 Subject: [PATCH 1/2] [Form] Add getLabel() and a label option on form fields --- src/Symfony/Component/Form/Field.php | 11 ++++++++++- src/Symfony/Component/Form/FieldInterface.php | 7 +++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Form/Field.php b/src/Symfony/Component/Form/Field.php index fbb3097c21c4a..29529b68c73d7 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') ?: $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. * From c14f9dac044cd3e3c4012ac051fa0b02e1b1a4d2 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 21 Feb 2011 19:10:41 +0100 Subject: [PATCH 2/2] [Form][TwigBundle] Make use of the new label option when rendering forms --- src/Symfony/Bundle/TwigBundle/Extension/FormExtension.php | 2 +- src/Symfony/Component/Form/Field.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 29529b68c73d7..a940b4b61f799 100644 --- a/src/Symfony/Component/Form/Field.php +++ b/src/Symfony/Component/Form/Field.php @@ -175,7 +175,7 @@ public function getName() */ public function getLabel() { - return $this->getOption('label') ?: $this->key; + return $this->getOption('label') ?: ucfirst(strtolower(str_replace('_', ' ', $this->key))); } /**