Skip to content

Commit 4c6d845

Browse files
committed
Changed labelTag to print empty if label empty
1 parent 85dfca9 commit 4c6d845

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/Fields/BoundField.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,16 @@ public function labelTag($contents = null, array $attrs = null)
8888
{
8989
$contents = is_null($contents) ? $this->label : $contents;
9090

91-
$widget = $this->field->getWidget();
91+
if (empty($contents)) {
92+
return "";
93+
}
9294

9395
if (!is_null($attrs)) {
9496
$attrs = Attributes::flatatt($attrs);
9597
}
9698

99+
$widget = $this->field->getWidget();
100+
97101
return Formatter::format(self::LABEL_TEMPLATE, array(
98102
"for" => $widget->buildAutoId($this->html_name),
99103
"attrs" => $attrs,

tests/unit/Fields/BoundFieldTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ public function testLabelTagWithContent()
134134
$this->assertXmlStringEqualsXmlString($bound->labelTag("content"), $expected);
135135
}
136136

137+
public function testLabelTagWithoutContent()
138+
{
139+
$field = new CharField(["label" => ""]);
140+
$bound = new BoundField($this->simple_form, $field, "name");
141+
142+
$this->assertEquals($bound->labelTag(), "");
143+
}
144+
137145
public function testLabelTagWithAttrs()
138146
{
139147
$bound = new BoundField($this->simple_form, $this->simple_field, "name");
@@ -143,6 +151,7 @@ public function testLabelTagWithAttrs()
143151
$this->assertXmlStringEqualsXmlString($bound->labelTag("content", $attrs), $expected);
144152
}
145153

154+
146155
public function testChoices()
147156
{
148157
$field = new ChoiceField(["choices" => array("option1" => "Option1", "option2" => "Option2")]);

0 commit comments

Comments
 (0)