Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

merged branch stloyd/feature/form-labels (PR #6262)

This PR was merged into the master branch.

Commits
-------

d5426f0 [Form] Add tests to prove that label is not rendered when is marked as false
120547c [Form][TwigBridge] Don't set label attributes if is marked as not to be rendered [Form][FrameworkBundle] Add option to disable rendering of label for fields
36e4556 [Form] Option for not displaying a label by setting label to false. [Form] Fixed formatting & translation ..

Discussion
----------

[Form] Added option for not displaying a form-label by setting label to false

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Replaces: #5421

@fabpot @BenjaminBeck: I was just not sure what to do with "table based" forms, so I left `<td></td>` rendered when there is no label, because I'm not sure that we can hide it easily.

---------------------------------------------------------------------------

by XWB at 2012-12-11T09:30:14Z

:+1:
  • Loading branch information...
commit 2578f1ef58ed27bf0e50a65e4019e830164317c4 2 parents 1072f0e + d5426f0
@fabpot fabpot authored
View
20 src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
@@ -221,16 +221,18 @@
{% block form_label %}
{% spaceless %}
- {% if not compound %}
- {% set label_attr = label_attr|merge({'for': id}) %}
- {% endif %}
- {% if required %}
- {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
- {% endif %}
- {% if label is empty %}
- {% set label = name|humanize %}
+ {% if label is not sameas(false) %}
+ {% if not compound %}
+ {% set label_attr = label_attr|merge({'for': id}) %}
+ {% endif %}
+ {% if required %}
+ {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
+ {% endif %}
+ {% if label is empty %}
+ {% set label = name|humanize %}
+ {% endif %}
+ <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}</label>
{% endif %}
- <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}</label>
{% endspaceless %}
{% endblock form_label %}
View
2  src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_label.html.php
@@ -1,4 +1,6 @@
+<?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); } ?>
<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 ?>
View
17 src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php
@@ -526,6 +526,23 @@ public function testLabelHasNoId()
);
}
+ public function testLabelIsNotRenderedWhenSetToFalse()
+ {
+ $form = $this->factory->createNamed('name', 'text', null, array(
+ 'label' => false
+ ));
+ $html = $this->renderRow($form->createView());
+
+ $this->assertMatchesXpath($html,
+'/div
+ [
+ ./input[@id="name"]
+ ]
+ [count(//label)=0]
+'
+ );
+ }
+
/**
* @dataProvider themeBlockInheritanceProvider
*/
View
19 src/Symfony/Component/Form/Tests/AbstractTableLayoutTest.php
@@ -39,6 +39,25 @@ public function testRow()
);
}
+ public function testLabelIsNotRenderedWhenSetToFalse()
+ {
+ $form = $this->factory->createNamed('name', 'text', null, array(
+ 'label' => false
+ ));
+ $html = $this->renderRow($form->createView());
+
+ $this->assertMatchesXpath($html,
+'/tr
+ [
+ ./td
+ [count(//label)=0]
+ /following-sibling::td
+ [./input[@id="name"]]
+ ]
+'
+ );
+ }
+
public function testRepeatedRow()
{
$form = $this->factory->createNamed('name', 'repeated');
Please sign in to comment.
Something went wrong with that request. Please try again.