Skip to content

[Fomr] Added label option to Form fields #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/TwigBundle/Extension/FormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
));
}

Expand Down
11 changes: 10 additions & 1 deletion src/Symfony/Component/Form/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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}
*/
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/Form/FieldInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down