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

minor improvements to form labels #5910

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/Zend/Form/Element.php
Expand Up @@ -17,7 +17,7 @@ class Element implements
ElementAttributeRemovalInterface,
ElementInterface,
InitializableInterface,
LabelOptionsAwareInterface
LabelAwareInterface
{
/**
* @var array
Expand Down
Expand Up @@ -9,8 +9,23 @@

namespace Zend\Form;

interface LabelOptionsAwareInterface
interface LabelAwareInterface
{
/**
* Set the label (if any) used for this element
*
* @param $label
* @return ElementInterface
*/
public function setLabel($label);

/**
* Retrieve the label (if any) used for this element
*
* @return string
*/
public function getLabel();

/**
* Set the attributes to use with the label
*
Expand Down
Expand Up @@ -9,7 +9,9 @@

namespace Zend\Form;

class LabelOptionsAwareTrait
use Traversable;

trait LabelAwareTrait
{
/**
* Label specific html attributes
Expand All @@ -29,7 +31,7 @@ class LabelOptionsAwareTrait
* Set the attributes to use with the label
*
* @param array $labelAttributes
* @return LabelOptionsAwareInterface
* @return LabelAwareInterface
*/
public function setLabelAttributes(array $labelAttributes)
{
Expand Down
10 changes: 4 additions & 6 deletions library/Zend/Form/View/Helper/FormButton.php
Expand Up @@ -11,7 +11,7 @@

use Zend\Form\ElementInterface;
use Zend\Form\Exception;
use Zend\Form\LabelOptionsAwareInterface;
use Zend\Form\LabelAwareInterface;

class FormButton extends FormInput
{
Expand Down Expand Up @@ -93,11 +93,9 @@ public function render(ElementInterface $element, $buttonContent = null)
}
}

if ($element instanceof LabelOptionsAwareInterface) {
if (! $element->getLabelOption('disable_html_escape')) {
$escapeHtmlHelper = $this->getEscapeHtmlHelper();
$buttonContent = $escapeHtmlHelper($buttonContent);
}
if (! $element instanceof LabelAwareInterface || ! $element->getLabelOption('disable_html_escape')) {
$escapeHtmlHelper = $this->getEscapeHtmlHelper();
$buttonContent = $escapeHtmlHelper($buttonContent);
}

return $openTag . $buttonContent . $this->closeTag();
Expand Down
17 changes: 8 additions & 9 deletions library/Zend/Form/View/Helper/FormCollection.php
Expand Up @@ -14,6 +14,7 @@
use Zend\Form\ElementInterface;
use Zend\Form\Element\Collection as CollectionElement;
use Zend\Form\FieldsetInterface;
use Zend\Form\LabelAwareInterface;
use Zend\View\Helper\AbstractHelper as BaseAbstractHelper;

class FormCollection extends AbstractHelper
Expand Down Expand Up @@ -101,10 +102,8 @@ public function render(ElementInterface $element)
return '';
}

$attributes = $element->getAttributes();
$markup = '';
$templateMarkup = '';
$escapeHtmlHelper = $this->getEscapeHtmlHelper();
$elementHelper = $this->getElementHelper();
$fieldsetHelper = $this->getFieldsetHelper();

Expand All @@ -124,16 +123,11 @@ public function render(ElementInterface $element)
if ($this->shouldWrap) {
$attributes = $element->getAttributes();
unset($attributes['name']);
$attributesString = '';
if (count($attributes)) {
$attributesString = ' ' . $this->createAttributesString($attributes);
}
$attributesString = count($attributes) ? ' ' . $this->createAttributesString($attributes) : '';

$label = $element->getLabel();
$legend = '';

$label = $element->getLabel();
$labelMarkup = '';
if (!empty($label)) {
if (null !== ($translator = $this->getTranslator())) {
$label = $translator->translate(
Expand All @@ -142,9 +136,14 @@ public function render(ElementInterface $element)
);
}

if (! $element instanceof LabelAwareInterface || ! $element->getLabelOption('disable_html_escape')) {
$escapeHtmlHelper = $this->getEscapeHtmlHelper();
$label = $escapeHtmlHelper($label);
}

$legend = sprintf(
$this->labelWrapper,
$escapeHtmlHelper($label)
$label
);
}

Expand Down
12 changes: 5 additions & 7 deletions library/Zend/Form/View/Helper/FormLabel.php
Expand Up @@ -11,7 +11,7 @@

use Zend\Form\ElementInterface;
use Zend\Form\Exception;
use Zend\Form\LabelOptionsAwareInterface;
use Zend\Form\LabelAwareInterface;

class FormLabel extends AbstractHelper
{
Expand Down Expand Up @@ -64,11 +64,9 @@ public function __invoke(ElementInterface $element = null, $labelContent = null,
);
}

if ($element instanceof LabelOptionsAwareInterface) {
if (! $element->getLabelOption('disable_html_escape')) {
$escapeHtmlHelper = $this->getEscapeHtmlHelper();
$label = $escapeHtmlHelper($label);
}
if (! $element instanceof LabelAwareInterface || ! $element->getLabelOption('disable_html_escape')) {
$escapeHtmlHelper = $this->getEscapeHtmlHelper();
$label = $escapeHtmlHelper($label);
}
}

Expand Down Expand Up @@ -127,7 +125,7 @@ public function openTag($attributesOrElement = null)
}

$labelAttributes = array();
if ($attributesOrElement instanceof LabelOptionsAwareInterface) {
if ($attributesOrElement instanceof LabelAwareInterface) {
$labelAttributes = $attributesOrElement->getLabelAttributes();
}

Expand Down
8 changes: 3 additions & 5 deletions library/Zend/Form/View/Helper/FormMultiCheckbox.php
Expand Up @@ -12,7 +12,7 @@
use Zend\Form\ElementInterface;
use Zend\Form\Element\MultiCheckbox as MultiCheckboxElement;
use Zend\Form\Exception;
use Zend\Form\LabelOptionsAwareInterface;
use Zend\Form\LabelAwareInterface;

class FormMultiCheckbox extends FormInput
{
Expand Down Expand Up @@ -149,15 +149,13 @@ protected function renderOptions(MultiCheckboxElement $element, array $options,
array $attributes)
{
$escapeHtmlHelper = $this->getEscapeHtmlHelper();
$labelOptions = array();
$labelHelper = $this->getLabelHelper();
$labelClose = $labelHelper->closeTag();
$labelPosition = $this->getLabelPosition();
$globalLabelAttributes = array();
$closingBracket = $this->getInlineClosingBracket();

if ($element instanceof LabelOptionsAwareInterface) {
$labelOptions = $element->getLabelOptions();
if ($element instanceof LabelAwareInterface) {
$globalLabelAttributes = $element->getLabelAttributes();
}

Expand Down Expand Up @@ -229,7 +227,7 @@ protected function renderOptions(MultiCheckboxElement $element, array $options,
);
}

if ($element instanceof LabelOptionsAwareInterface && !$element->getLabelOption('disable_html_escape')) {
if (! $element instanceof LabelAwareInterface || ! $element->getLabelOption('disable_html_escape')) {
$label = $escapeHtmlHelper($label);
}

Expand Down
16 changes: 7 additions & 9 deletions library/Zend/Form/View/Helper/FormRow.php
Expand Up @@ -12,7 +12,7 @@
use Zend\Form\Element\Button;
use Zend\Form\ElementInterface;
use Zend\Form\Exception;
use Zend\Form\LabelOptionsAwareInterface;
use Zend\Form\LabelAwareInterface;

class FormRow extends AbstractHelper
{
Expand Down Expand Up @@ -161,16 +161,14 @@ public function render(ElementInterface $element)

if (isset($label) && '' !== $label) {

$labelOptions = array();
$labelAttributes = array();

if ($element instanceof LabelOptionsAwareInterface) {
$labelOptions = $element->getLabelOptions();
if ($element instanceof LabelAwareInterface) {
$labelAttributes = $element->getLabelAttributes();
}

if (! $element->getLabelOption('disable_html_escape')) {
$label = $escapeHtmlHelper($label);
}
if (! $element instanceof LabelAwareInterface || ! $element->getLabelOption('disable_html_escape')) {
$label = $escapeHtmlHelper($label);
}

if (empty($labelAttributes)) {
Expand All @@ -189,7 +187,7 @@ public function render(ElementInterface $element)
// Ensure element and label will be separated if element has an `id`-attribute.
// If element has label option `always_wrap` it will be nested in any case.
if ($element->hasAttribute('id')
&& ($element instanceof LabelOptionsAwareInterface && !$element->getLabelOption('always_wrap'))
&& ($element instanceof LabelAwareInterface && !$element->getLabelOption('always_wrap'))
) {
$labelOpen = '';
$labelClose = '';
Expand All @@ -200,7 +198,7 @@ public function render(ElementInterface $element)
}

if ($label !== '' && (!$element->hasAttribute('id'))
|| ($element instanceof LabelOptionsAwareInterface && $element->getLabelOption('always_wrap'))
|| ($element instanceof LabelAwareInterface && $element->getLabelOption('always_wrap'))
) {
$label = '<span>' . $label . '</span>';
}
Expand Down
19 changes: 19 additions & 0 deletions tests/ZendTest/Form/View/Helper/FormCollectionTest.php
Expand Up @@ -363,4 +363,23 @@ public function testGetterAndSetter()
$this->assertAttributeEquals('foo', 'templateWrapper', $this->helper);
$this->assertEquals('foo', $this->helper->getTemplateWrapper());
}

public function testLabelIsEscapedByDefault()
{
$form = $this->getForm();
$collection = $form->get('colors');
$collection->setLabel('<strong>Some label</strong>');
$markup = $this->helper->render($collection);
$this->assertRegexp('#<fieldset(.*?)><legend>&lt;strong&gt;Some label&lt;/strong&gt;<\/legend>(.*?)<\/fieldset>#', $markup);
}

public function testCanDisableLabelHtmlEscape()
{
$form = $this->getForm();
$collection = $form->get('colors');
$collection->setLabel('<strong>Some label</strong>');
$collection->setLabelOptions(array('disable_html_escape' => true));
$markup = $this->helper->render($collection);
$this->assertRegexp('#<fieldset(.*?)><legend><strong>Some label</strong><\/legend>(.*?)<\/fieldset>#', $markup);
}
}