Przeglądaj źródła

ZF-11225: Make escaping label optional in FormErrors form decorator

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24869 44c647ce-9c0f-0410-b52a-842ac1e357ba
adamlundrigan 13 lat temu
rodzic
commit
278d670b29

+ 46 - 1
library/Zend/Form/Decorator/FormErrors.php

@@ -70,6 +70,12 @@ class Zend_Form_Decorator_FormErrors extends Zend_Form_Decorator_Abstract
     /**#@-*/
 
     /**
+     * Whether or not to escape error label and error message
+     * @var bool
+     */
+    protected $_escape;
+
+    /**
      * Render errors
      *
      * @param  string $content
@@ -405,6 +411,41 @@ class Zend_Form_Decorator_FormErrors extends Zend_Form_Decorator_Abstract
     }
 
     /**
+     * Set whether or not to escape error label and error message
+     *
+     * Sets also the 'escape' option for the view helper
+     *
+     * @param  bool $flag
+     * @return Zend_Form_Decorator_FormErrors
+     */
+    public function setEscape($flag)
+    {
+        $this->_escape = (bool) $flag;
+
+        // Set also option for view helper
+        $this->setOption('escape', $this->_escape);
+        return $this;
+    }
+
+    /**
+     * Get escape flag
+     *
+     * @return bool
+     */
+    public function getEscape()
+    {
+        if (null === $this->_escape) {
+            if (null !== ($escape = $this->getOption('escape'))) {
+                $this->setEscape($escape);
+            } else {
+                $this->setEscape(true);
+            }
+        }
+
+        return $this->_escape;
+    }
+
+    /**
      * Render element label
      *
      * @param  Zend_Form_Element $element
@@ -418,8 +459,12 @@ class Zend_Form_Decorator_FormErrors extends Zend_Form_Decorator_Abstract
             $label = $element->getName();
         }
 
+        if ($this->getEscape()) {
+            $label = $view->escape($label);
+        }
+
         return $this->getMarkupElementLabelStart()
-             . $view->escape($label)
+             . $label
              . $this->getMarkupElementLabelEnd();
     }
 

+ 33 - 0
tests/Zend/Form/Decorator/FormErrorsTest.php

@@ -329,6 +329,39 @@ class Zend_Form_Decorator_FormErrorsTest extends PHPUnit_Framework_TestCase
         $this->assertTrue($this->decorator->getShowCustomFormErrors());
     }
 
+    /**
+     * @group ZF-11225
+     */
+    public function testRenderingEscapesFormErrorsByDefault()
+    {
+        $this->setupForm();
+        $this->form->addDecorator($this->decorator)
+                   ->addError('<strong>form-badness</strong>');
+        $html = $this->form->render();
+        $this->assertContains('&lt;strong&gt;form-badness&lt;/strong&gt;', $html);
+    }
+
+    /**
+     * @group ZF-11225
+     */
+    public function testCanDisableEscapingFormErrors()
+    {
+        $this->setupForm();
+        $this->form->addDecorator($this->decorator);
+
+        // Set error message with html content
+        $this->form->addError('<strong>form-badness</strong>');
+
+        // Set element label with html content
+        $this->form->getElement('bar')->setLabel('<strong>Sub Bar: </strong>');
+
+        $this->form->getDecorator('FormErrors')->setEscape(false);
+
+        $html = $this->form->render();
+        $this->assertContains('<li><strong>form-badness</strong>', $html);
+        $this->assertContains('<li><b><strong>Sub Bar: </strong>', $html);
+    }
+
     public function markupOptionMethodsProvider()
     {
         return array(