Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2578f1e

Browse files
committed
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:
2 parents 1072f0e + d5426f0 commit 2578f1e

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,18 @@
221221

222222
{% block form_label %}
223223
{% spaceless %}
224-
{% if not compound %}
225-
{% set label_attr = label_attr|merge({'for': id}) %}
226-
{% endif %}
227-
{% if required %}
228-
{% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
229-
{% endif %}
230-
{% if label is empty %}
231-
{% set label = name|humanize %}
224+
{% if label is not sameas(false) %}
225+
{% if not compound %}
226+
{% set label_attr = label_attr|merge({'for': id}) %}
227+
{% endif %}
228+
{% if required %}
229+
{% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
230+
{% endif %}
231+
{% if label is empty %}
232+
{% set label = name|humanize %}
233+
{% endif %}
234+
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}</label>
232235
{% endif %}
233-
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}</label>
234236
{% endspaceless %}
235237
{% endblock form_label %}
236238

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
<?php if (false !== $label): ?>
12
<?php if ($required) { $label_attr['class'] = trim((isset($label_attr['class']) ? $label_attr['class'] : '').' required'); } ?>
23
<?php if (!$compound) { $label_attr['for'] = $id; } ?>
34
<?php if (!$label) { $label = $view['form']->humanize($name); } ?>
45
<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>
6+
<?php endif ?>

src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,23 @@ public function testLabelHasNoId()
526526
);
527527
}
528528

529+
public function testLabelIsNotRenderedWhenSetToFalse()
530+
{
531+
$form = $this->factory->createNamed('name', 'text', null, array(
532+
'label' => false
533+
));
534+
$html = $this->renderRow($form->createView());
535+
536+
$this->assertMatchesXpath($html,
537+
'/div
538+
[
539+
./input[@id="name"]
540+
]
541+
[count(//label)=0]
542+
'
543+
);
544+
}
545+
529546
/**
530547
* @dataProvider themeBlockInheritanceProvider
531548
*/

src/Symfony/Component/Form/Tests/AbstractTableLayoutTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,25 @@ public function testRow()
3939
);
4040
}
4141

42+
public function testLabelIsNotRenderedWhenSetToFalse()
43+
{
44+
$form = $this->factory->createNamed('name', 'text', null, array(
45+
'label' => false
46+
));
47+
$html = $this->renderRow($form->createView());
48+
49+
$this->assertMatchesXpath($html,
50+
'/tr
51+
[
52+
./td
53+
[count(//label)=0]
54+
/following-sibling::td
55+
[./input[@id="name"]]
56+
]
57+
'
58+
);
59+
}
60+
4261
public function testRepeatedRow()
4362
{
4463
$form = $this->factory->createNamed('name', 'repeated');

0 commit comments

Comments
 (0)