Skip to content

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

Merged
merged 3 commits into from Dec 11, 2012
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');
Something went wrong with that request. Please try again.