Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Added ability to specify label position as an element label option #6752

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion library/Zend/Form/View/Helper/FormRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,13 @@ public function render(ElementInterface $element)
$labelOpen = $labelClose = $label = '';
}

switch ($this->labelPosition) {
$labelPosition = $this->labelPosition;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line caused two test failures. Essentially, it was overriding lines 128-130, which were setting the value of $labelPosition to the instance property if it had not been passed. As such, I removed it, as the value will be properly initialized by this point already.


if ($element instanceof LabelAwareInterface && $element->getLabelOption('label_position')) {
$labelPosition = $element->getLabelOption('label_position');
}

switch ($labelPosition) {
case self::LABEL_PREPEND:
$markup = $labelOpen . $label . $elementString . $labelClose;
break;
Expand Down
26 changes: 26 additions & 0 deletions tests/ZendTest/Form/View/Helper/FormRowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,32 @@ public function testCanCreateLabelValueAfterInput()
$this->assertContains('</label>', $markup);
}

public function testCanOverrideLabelPosition()
{
$fooElement = new Element('foo');
$fooElement->setOptions(array(
'label' => 'The value for foo:',
'label_options' => array(
'label_position' =>'prepend'
),
));

$barElement = new Element('bar');
$barElement->setOptions(array(
'label' => 'The value for bar:',
));

$this->helper->setLabelPosition('append');

$fooMarkup = $this->helper->render($fooElement);
$this->assertContains('<label><span>The value for foo:</span><', $fooMarkup);
$this->assertContains('</label>', $fooMarkup);

$barMarkup = $this->helper->render($barElement);
$this->assertContains('<label><', $barMarkup);
$this->assertContains('<span>The value for bar:</span></label>', $barMarkup);
}

public function testCanRenderRowLabelAttributes()
{
$element = new Element('foo');
Expand Down