8000 [Form] Added "label_format" option by webmozart · Pull Request #12050 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Added "label_format" option #12050

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

Merged
merged 1 commit into from
Sep 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments. Retry
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,14 @@

{% block button_widget -%}
{% if label is empty -%}
{% set label = name|humanize %}
{%- if label_format is not empty -%}
{% set label = label_format|replace({
'%name%': name,
'%id%': id,
}) %}
{%- else -%}
{% set label = name|humanize %}
{%- endif -%}
{%- endif -%}
<button type="{{ type|default('button') }}" {{ block('button_attributes') }}>{{ label|trans({}, translation_domain) }}</button>
{%- endblock button_widget %}
Expand All @@ -203,7 +210,14 @@
{% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
{%- endif %}
{% if label is empty -%}
{% set label = name|humanize %}
{%- if label_format is not empty -%}
{% set label = label_format|replace({
'%name%': name,
'%id%': id,
}) %}
{%- else -%}
{% set label = name|humanize %}
{%- endif -%}
{%- endif -%}
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}</label>
{%- endif %}
Expand Down
8000
Original file line number Diff line numberDiff line change
@@ -1,2 +1,4 @@
<?php if (!$label) { $label = $view['form']->humanize($name); } ?>
<?php if (!$label) { $label = isset($label_format)
? strtr($label_format, array('%name%' => $name, '%id%' => $id))
: $view['form']->humanize($name); } ?>
<button type="<?php echo isset($type) ? $view->escape($type) : 'button' ?>" <?php echo $view['form']->block($form, 'button_attributes') ?>><?php echo $view->escape($view['translator']->trans($label, array(), $translation_domain)) ?></button>
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php if (false !== $label): ?>
<?php if ($required) { $label_attr['class'] = trim((isset($label_attr['class']) ? $label_attr['class'] : '').' required'); } ?>
<?php if (!$compound) { $label_attr['for'] = $id; } ?>
<?php if (!$label) { $label = $view['form']->humanize($name); } ?>
<?php if (!$label) { $label = isset($label_format)
? strtr($label_format, array('%name%' => $name, '%id%' => $id))
: $view['form']->humanize($name); } ?>
<label <?php foreach ($label_attr as $k => $v) { printf('%s="%s" ', $view->escape($k), $view->escape($v)); } ?>><?php echo $view->escape($view['translator']->trans($label, array(), $translation_domain)) ?></label>
<?php endif ?>
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* added "html5" option to Date, Time and DateTimeFormType to be able to
enable/disable HTML5 input date when widget option is "single_text"
* added "label_format" option with possible placeholders "%name%" and "%id%"

2.5.0
------
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
$name = $form->getName();
$blockName = $options['block_name'] ?: $form->getName();
$translationDomain = $options['translation_domain'];
$labelFormat = $options['label_format'];

if ($view->parent) {
if ('' !== ($parentFullName = $view->parent->vars['full_name'])) {
Expand All @@ -59,6 +60,10 @@ public function buildView(FormView $view, FormInterface $form, array $options)
if (!$translationDomain) {
$translationDomain = $view->parent->vars['translation_domain'];
}

if (!$labelFormat) {
$labelFormat = $view->parent->vars['label_format'];
}
} else {
$id = $name;
$fullName = $name;
Expand Down Expand Up @@ -87,6 +92,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
'full_name' => $fullName,
'disabled' => $form->isDisabled(),
'label' => $options['label'],
'label_format' => $labelFormat,
'multipart' => false,
'attr' => $options['attr'],
'block_prefixes' => $blockPrefixes,
Expand All @@ -111,6 +117,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'block_name' => null,
'disabled' => false,
'label' => null,
'label_format' => null,
'attr' => array(),
'translation_domain' => null,
'auto_initialize' => true,
Expand Down
102 changes: 102 additions & 0 deletions src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,108 @@ public function testLabelWithCustomTextAsOptionAndCustomAttributesPassedDirectly
);
}

public function testLabelFormatName()
{
$form = $this->factory->createNamedBuilder('myform')
->add('myfield', 'text')
->getForm();
$view = $form->get('myfield')->createView();
$html = $this->renderLabel($view, null, array('label_format' => 'form.%name%'));

$this->assertMatchesXpath($html,
'/label
[@for="myform_myfield"]
[.="[trans]form.myfield[/trans]"]
'
);
}

public function testLabelFormatId()
{
$form = $this->factory->createNamedBuilder('myform')
->add('myfield', 'text')
->getForm();
$view = $form->get('myfield')->createView();
$html = $this->renderLabel($view, null, array('label_format' => 'form.%id%'));

$this->assertMatchesXpath($html,
'/label
[@for="myform_myfield"]
[.="[trans]form.myform_myfield[/trans]"]
'
);
}

public function testLabelFormatAsFormOption()
{
$options = array('label_format' => 'form.%name%');

$form = $this->factory->createNamedBuilder('myform', 'form', null, $options)
->add('myfield', 'text')
->getForm();
$view = $form->get('myfield')->createView();
$html = $this->renderLabel($view);

$this->assertMatchesXpath($html,
'/label
[@for="myform_myfield"]
[.="[trans]form.myfield[/trans]"]
'
);
}

public function testLabelFormatOverriddenOption()
{
$options = array('label_format' => 'form.%name%');

$form = $this->factory->createNamedBuilder('myform', 'form', null, $options)
->add('myfield', 'text', array('label_format' => 'field.%name%'))
->getForm();
$view = $form->get('myfield')->createView();
$html = $this->renderLabel($view);

$this->assertMatchesXpath($html,
'/label
[@for="myform_myfield"]
[.="[trans]field.myfield[/trans]"]
'
);
}

public function testLabelFormatOnButton()
{
$form = $this->factory->createNamedBuilder('myform')
->add('mybutton', 'button')
->getForm();
$view = $form->get('mybutton')->createView();
$html = $this->renderWidget($view, array('label_format' => 'form.%name%'));

$this->assertMatchesXpath($html,
'/button
[@type="button"]
[@name="myform[mybutton]"]
[.="[trans]form.mybutton[/trans]"]
'
);
}

public function testLabelFormatOnButtonId()
{
$form = $this->factory->createNamedBuilder('myform')
->add('mybutton', 'button')
->getForm();
$view = $form->get('mybutton')->createView();
$html = $this->renderWidget($view, array('label_format' => 'form.%id%'));

$this->assertMatchesXpath($html,
'/button
[@type="button"]
[@name="myform[mybutton]"]
[.="[trans]form.myform_mybutton[/trans]"]
'
);
}

public function testErrors()
{
$form = $this->factory->createNamed('name', 'text');
Expand Down
0