File indexing completed on 2024-05-26 06:03:00

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Form
0017  * @subpackage Decorator
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  */
0021 
0022 /** Zend_Form_Decorator_Abstract */
0023 // require_once 'Zend/Form/Decorator/Abstract.php';
0024 
0025 /**
0026  * Zend_Form_Decorator_FormErrors
0027  *
0028  * Displays all form errors in one view.
0029  *
0030  * Any options passed will be used as HTML attributes of the ul tag for the errors.
0031  *
0032  * @category   Zend
0033  * @package    Zend_Form
0034  * @subpackage Decorator
0035  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0036  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0037  * @version    $Id$
0038  */
0039 class Zend_Form_Decorator_FormErrors extends Zend_Form_Decorator_Abstract
0040 {
0041     /**
0042      * Default values for markup options
0043      * @var array
0044      */
0045     protected $_defaults = array(
0046         'ignoreSubForms'          => false,
0047         'showCustomFormErrors'    => true,
0048         'onlyCustomFormErrors'    => false,
0049         'markupElementLabelEnd'   => '</b>',
0050         'markupElementLabelStart' => '<b>',
0051         'markupListEnd'           => '</ul>',
0052         'markupListItemEnd'       => '</li>',
0053         'markupListItemStart'     => '<li>',
0054         'markupListStart'         => '<ul class="form-errors">',
0055     );
0056 
0057     /**#@+
0058      * Markup options
0059      * @var string
0060      */
0061     protected $_ignoreSubForms;
0062     protected $_showCustomFormErrors;
0063     protected $_onlyCustomFormErrors;
0064     protected $_markupElementLabelEnd;
0065     protected $_markupElementLabelStart;
0066     protected $_markupListEnd;
0067     protected $_markupListItemEnd;
0068     protected $_markupListItemStart;
0069     protected $_markupListStart;
0070     /**#@-*/
0071 
0072     /**
0073      * Whether or not to escape error label and error message
0074      * @var bool
0075      */
0076     protected $_escape;
0077 
0078     /**
0079      * Render errors
0080      *
0081      * @param  string $content
0082      * @return string
0083      */
0084     public function render($content)
0085     {
0086         $form = $this->getElement();
0087         if (!$form instanceof Zend_Form) {
0088             return $content;
0089         }
0090 
0091         $view = $form->getView();
0092         if (null === $view) {
0093             return $content;
0094         }
0095 
0096         $this->initOptions();
0097         $markup = $this->_recurseForm($form, $view);
0098 
0099         if (empty($markup)) {
0100             return $content;
0101         }
0102 
0103         $markup = $this->getMarkupListStart()
0104                 . $markup
0105                 . $this->getMarkupListEnd();
0106 
0107         switch ($this->getPlacement()) {
0108             case self::APPEND:
0109                 return $content . $this->getSeparator() . $markup;
0110             case self::PREPEND:
0111                 return $markup . $this->getSeparator() . $content;
0112         }
0113     }
0114 
0115     /**
0116      * Initialize options
0117      *
0118      * @return void
0119      */
0120     public function initOptions()
0121     {
0122         $this->getMarkupElementLabelEnd();
0123         $this->getMarkupElementLabelStart();
0124         $this->getMarkupListEnd();
0125         $this->getMarkupListItemEnd();
0126         $this->getMarkupListItemStart();
0127         $this->getMarkupListStart();
0128         $this->getPlacement();
0129         $this->getSeparator();
0130         $this->ignoreSubForms();
0131         $this->getShowCustomFormErrors();
0132         $this->getOnlyCustomFormErrors();
0133     }
0134 
0135     /**
0136      * Retrieve markupElementLabelStart
0137      *
0138      * @return string
0139      */
0140     public function getMarkupElementLabelStart()
0141     {
0142         if (null === $this->_markupElementLabelStart) {
0143             if (null === ($markupElementLabelStart = $this->getOption('markupElementLabelStart'))) {
0144                 $this->setMarkupElementLabelStart($this->_defaults['markupElementLabelStart']);
0145             } else {
0146                 $this->setMarkupElementLabelStart($markupElementLabelStart);
0147                 $this->removeOption('markupElementLabelStart');
0148             }
0149         }
0150 
0151         return $this->_markupElementLabelStart;
0152     }
0153 
0154     /**
0155      * Set markupElementLabelStart
0156      *
0157      * @param  string $markupElementLabelStart
0158      * @return Zend_Form_Decorator_FormErrors
0159      */
0160     public function setMarkupElementLabelStart($markupElementLabelStart)
0161     {
0162         $this->_markupElementLabelStart = $markupElementLabelStart;
0163         return $this;
0164     }
0165 
0166     /**
0167      * Retrieve markupElementLabelEnd
0168      *
0169      * @return string
0170      */
0171     public function getMarkupElementLabelEnd()
0172     {
0173         if (null === $this->_markupElementLabelEnd) {
0174             if (null === ($markupElementLabelEnd = $this->getOption('markupElementLabelEnd'))) {
0175                 $this->setMarkupElementLabelEnd($this->_defaults['markupElementLabelEnd']);
0176             } else {
0177                 $this->setMarkupElementLabelEnd($markupElementLabelEnd);
0178                 $this->removeOption('markupElementLabelEnd');
0179             }
0180         }
0181 
0182         return $this->_markupElementLabelEnd;
0183     }
0184 
0185     /**
0186      * Set markupElementLabelEnd
0187      *
0188      * @param  string $markupElementLabelEnd
0189      * @return Zend_Form_Decorator_FormErrors
0190      */
0191     public function setMarkupElementLabelEnd($markupElementLabelEnd)
0192     {
0193         $this->_markupElementLabelEnd = $markupElementLabelEnd;
0194         return $this;
0195     }
0196 
0197     /**
0198      * Retrieve markupListStart
0199      *
0200      * @return string
0201      */
0202     public function getMarkupListStart()
0203     {
0204         if (null === $this->_markupListStart) {
0205             if (null === ($markupListStart = $this->getOption('markupListStart'))) {
0206                 $this->setMarkupListStart($this->_defaults['markupListStart']);
0207             } else {
0208                 $this->setMarkupListStart($markupListStart);
0209                 $this->removeOption('markupListStart');
0210             }
0211         }
0212 
0213         return $this->_markupListStart;
0214     }
0215 
0216     /**
0217      * Set markupListStart
0218      *
0219      * @param  string $markupListStart
0220      * @return Zend_Form_Decorator_FormErrors
0221      */
0222     public function setMarkupListStart($markupListStart)
0223     {
0224         $this->_markupListStart = $markupListStart;
0225         return $this;
0226     }
0227 
0228     /**
0229      * Retrieve markupListEnd
0230      *
0231      * @return string
0232      */
0233     public function getMarkupListEnd()
0234     {
0235         if (null === $this->_markupListEnd) {
0236             if (null === ($markupListEnd = $this->getOption('markupListEnd'))) {
0237                 $this->setMarkupListEnd($this->_defaults['markupListEnd']);
0238             } else {
0239                 $this->setMarkupListEnd($markupListEnd);
0240                 $this->removeOption('markupListEnd');
0241             }
0242         }
0243 
0244         return $this->_markupListEnd;
0245     }
0246 
0247     /**
0248      * Set markupListEnd
0249      *
0250      * @param  string $markupListEnd
0251      * @return Zend_Form_Decorator_FormErrors
0252      */
0253     public function setMarkupListEnd($markupListEnd)
0254     {
0255         $this->_markupListEnd = $markupListEnd;
0256         return $this;
0257     }
0258 
0259     /**
0260      * Retrieve markupListItemStart
0261      *
0262      * @return string
0263      */
0264     public function getMarkupListItemStart()
0265     {
0266         if (null === $this->_markupListItemStart) {
0267             if (null === ($markupListItemStart = $this->getOption('markupListItemStart'))) {
0268                 $this->setMarkupListItemStart($this->_defaults['markupListItemStart']);
0269             } else {
0270                 $this->setMarkupListItemStart($markupListItemStart);
0271                 $this->removeOption('markupListItemStart');
0272             }
0273         }
0274 
0275         return $this->_markupListItemStart;
0276     }
0277 
0278     /**
0279      * Set markupListItemStart
0280      *
0281      * @param  string $markupListItemStart
0282      * @return Zend_Form_Decorator_FormErrors
0283      */
0284     public function setMarkupListItemStart($markupListItemStart)
0285     {
0286         $this->_markupListItemStart = $markupListItemStart;
0287         return $this;
0288     }
0289 
0290     /**
0291      * Retrieve markupListItemEnd
0292      *
0293      * @return string
0294      */
0295     public function getMarkupListItemEnd()
0296     {
0297         if (null === $this->_markupListItemEnd) {
0298             if (null === ($markupListItemEnd = $this->getOption('markupListItemEnd'))) {
0299                 $this->setMarkupListItemEnd($this->_defaults['markupListItemEnd']);
0300             } else {
0301                 $this->setMarkupListItemEnd($markupListItemEnd);
0302                 $this->removeOption('markupListItemEnd');
0303             }
0304         }
0305 
0306         return $this->_markupListItemEnd;
0307     }
0308 
0309     /**
0310      * Set markupListItemEnd
0311      *
0312      * @param  string $markupListItemEnd
0313      * @return Zend_Form_Decorator_FormErrors
0314      */
0315     public function setMarkupListItemEnd($markupListItemEnd)
0316     {
0317         $this->_markupListItemEnd = $markupListItemEnd;
0318         return $this;
0319     }
0320 
0321     /**
0322      * Retrieve ignoreSubForms
0323      *
0324      * @return bool
0325      */
0326     public function ignoreSubForms()
0327     {
0328         if (null === $this->_ignoreSubForms) {
0329             if (null === ($ignoreSubForms = $this->getOption('ignoreSubForms'))) {
0330                 $this->setIgnoreSubForms($this->_defaults['ignoreSubForms']);
0331             } else {
0332                 $this->setIgnoreSubForms($ignoreSubForms);
0333                 $this->removeOption('ignoreSubForms');
0334             }
0335         }
0336 
0337         return $this->_ignoreSubForms;
0338     }
0339 
0340     /**
0341      * Set ignoreSubForms
0342      *
0343      * @param  bool $ignoreSubForms
0344      * @return Zend_Form_Decorator_FormErrors
0345      */
0346     public function setIgnoreSubForms($ignoreSubForms)
0347     {
0348         $this->_ignoreSubForms = (bool) $ignoreSubForms;
0349         return $this;
0350     }
0351 
0352     /**
0353      * Get showCustomFormErrors
0354      *
0355      * @return bool
0356      */
0357     public function getShowCustomFormErrors()
0358     {
0359         if (null === $this->_showCustomFormErrors) {
0360             if (null === ($show =  $this->getOption('showCustomFormErrors'))) {
0361                 $this->setShowCustomFormErrors($this->_defaults['showCustomFormErrors']);
0362             } else {
0363                 $this->setShowCustomFormErrors($show);
0364                 $this->removeOption('showCustomFormErrors');
0365             }
0366         }
0367         return $this->_showCustomFormErrors;
0368     }
0369 
0370     /**
0371      * Set showCustomFormErrors
0372      *
0373      * @param  bool $showCustomFormErrors
0374      * @return Zend_Form_Decorator_FormErrors
0375      */
0376     public function setShowCustomFormErrors($showCustomFormErrors)
0377     {
0378         $this->_showCustomFormErrors = (bool)$showCustomFormErrors;
0379         return $this;
0380     }
0381 
0382     /**
0383      * Get onlyCustomFormErrors
0384      *
0385      * @return bool
0386      */
0387     public function getOnlyCustomFormErrors()
0388     {
0389         if (null === $this->_onlyCustomFormErrors) {
0390             if (null === ($show =  $this->getOption('onlyCustomFormErrors'))) {
0391                 $this->setOnlyCustomFormErrors($this->_defaults['onlyCustomFormErrors']);
0392             } else {
0393                 $this->setOnlyCustomFormErrors($show);
0394                 $this->removeOption('onlyCustomFormErrors');
0395             }
0396         }
0397         return $this->_onlyCustomFormErrors;
0398     }
0399 
0400     /**
0401      * Set onlyCustomFormErrors, whether to display elements messages
0402      * in addition to custom form messages.
0403      *
0404      * @param  bool $onlyCustomFormErrors
0405      * @return Zend_Form_Decorator_FormErrors
0406      */
0407     public function setOnlyCustomFormErrors($onlyCustomFormErrors)
0408     {
0409         $this->_onlyCustomFormErrors = (bool)$onlyCustomFormErrors;
0410         return $this;
0411     }
0412 
0413     /**
0414      * Set whether or not to escape error label and error message
0415      *
0416      * Sets also the 'escape' option for the view helper
0417      *
0418      * @param  bool $flag
0419      * @return Zend_Form_Decorator_FormErrors
0420      */
0421     public function setEscape($flag)
0422     {
0423         $this->_escape = (bool) $flag;
0424 
0425         // Set also option for view helper
0426         $this->setOption('escape', $this->_escape);
0427         return $this;
0428     }
0429 
0430     /**
0431      * Get escape flag
0432      *
0433      * @return bool
0434      */
0435     public function getEscape()
0436     {
0437         if (null === $this->_escape) {
0438             if (null !== ($escape = $this->getOption('escape'))) {
0439                 $this->setEscape($escape);
0440             } else {
0441                 $this->setEscape(true);
0442             }
0443         }
0444 
0445         return $this->_escape;
0446     }
0447 
0448     /**
0449      * Render element label
0450      *
0451      * @param  Zend_Form_Element $element
0452      * @param  Zend_View_Interface $view
0453      * @return string
0454      */
0455     public function renderLabel(Zend_Form_Element $element, Zend_View_Interface $view)
0456     {
0457         $label = $element->getLabel();
0458         if (empty($label)) {
0459             $label = $element->getName();
0460 
0461             // Translate element name
0462             if (null !== ($translator = $element->getTranslator())) {
0463                 $label = $translator->translate($label);
0464             }
0465         }
0466 
0467         if ($this->getEscape()) {
0468             $label = $view->escape($label);
0469         }
0470 
0471         return $this->getMarkupElementLabelStart()
0472              . $label
0473              . $this->getMarkupElementLabelEnd();
0474     }
0475 
0476     /**
0477      * Recurse through a form object, rendering errors
0478      *
0479      * @param  Zend_Form $form
0480      * @param  Zend_View_Interface $view
0481      * @return string
0482      */
0483     protected function _recurseForm(Zend_Form $form, Zend_View_Interface $view)
0484     {
0485         $content = '';
0486 
0487         $custom = $form->getCustomMessages();
0488         if ($this->getShowCustomFormErrors() && count($custom)) {
0489             $content .= $this->getMarkupListItemStart()
0490                      .  $view->formErrors($custom, $this->getOptions())
0491                      .  $this->getMarkupListItemEnd();
0492         }
0493         foreach ($form->getElementsAndSubFormsOrdered() as $subitem) {
0494             if ($subitem instanceof Zend_Form_Element && !$this->getOnlyCustomFormErrors()) {
0495                 $messages = $subitem->getMessages();
0496                 if (count($messages)) {
0497                     $subitem->setView($view);
0498                     $content .= $this->getMarkupListItemStart()
0499                              .  $this->renderLabel($subitem, $view)
0500                              .  $view->formErrors($messages, $this->getOptions())
0501                              .  $this->getMarkupListItemEnd();
0502                 }
0503             } else if ($subitem instanceof Zend_Form && !$this->ignoreSubForms()) {
0504                 $markup = $this->_recurseForm($subitem, $view);
0505 
0506                 if (!empty($markup)) {
0507                     $content .= $this->getMarkupListStart()
0508                               . $markup
0509                               . $this->getMarkupListEnd();
0510                 }
0511             }
0512         }
0513         return $content;
0514     }
0515 }