web/lib/Zend/View/Helper/FormErrors.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_View
       
    17  * @subpackage Helper
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: FormErrors.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /**
       
    24  * Abstract class for extension
       
    25  */
       
    26 require_once 'Zend/View/Helper/FormElement.php';
       
    27 
       
    28 
       
    29 /**
       
    30  * Helper to render errors for a form element
       
    31  *
       
    32  * @category   Zend
       
    33  * @package    Zend_View
       
    34  * @subpackage Helper
       
    35  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    36  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    37  */
       
    38 class Zend_View_Helper_FormErrors extends Zend_View_Helper_FormElement
       
    39 {
       
    40     /**
       
    41      * @var Zend_Form_Element
       
    42      */
       
    43     protected $_element;
       
    44 
       
    45     /**#@+
       
    46      * @var string Element block start/end tags and separator
       
    47      */
       
    48     protected $_htmlElementEnd       = '</li></ul>';
       
    49     protected $_htmlElementStart     = '<ul%s><li>';
       
    50     protected $_htmlElementSeparator = '</li><li>';
       
    51     /**#@-*/
       
    52 
       
    53     /**
       
    54      * Render form errors
       
    55      *
       
    56      * @param  string|array $errors Error(s) to render
       
    57      * @param  array $options
       
    58      * @return string
       
    59      */
       
    60     public function formErrors($errors, array $options = null)
       
    61     {
       
    62         $escape = true;
       
    63         if (isset($options['escape'])) {
       
    64             $escape = (bool) $options['escape'];
       
    65             unset($options['escape']);
       
    66         }
       
    67 
       
    68         if (empty($options['class'])) {
       
    69             $options['class'] = 'errors';
       
    70         }
       
    71 
       
    72         $start = $this->getElementStart();
       
    73         if (strstr($start, '%s')) {
       
    74             $attribs = $this->_htmlAttribs($options);
       
    75             $start   = sprintf($start, $attribs);
       
    76         }
       
    77 
       
    78         if ($escape) {
       
    79             foreach ($errors as $key => $error) {
       
    80                 $errors[$key] = $this->view->escape($error);
       
    81             }
       
    82         }
       
    83 
       
    84         $html  = $start
       
    85                . implode($this->getElementSeparator(), (array) $errors)
       
    86                . $this->getElementEnd();
       
    87 
       
    88         return $html;
       
    89     }
       
    90 
       
    91     /**
       
    92      * Set end string for displaying errors
       
    93      *
       
    94      * @param  string $string
       
    95      * @return Zend_View_Helper_FormErrors
       
    96      */
       
    97     public function setElementEnd($string)
       
    98     {
       
    99         $this->_htmlElementEnd = (string) $string;
       
   100         return $this;
       
   101     }
       
   102 
       
   103     /**
       
   104      * Retrieve end string for displaying errors
       
   105      *
       
   106      * @return string
       
   107      */
       
   108     public function getElementEnd()
       
   109     {
       
   110         return $this->_htmlElementEnd;
       
   111     }
       
   112 
       
   113     /**
       
   114      * Set separator string for displaying errors
       
   115      *
       
   116      * @param  string $string
       
   117      * @return Zend_View_Helper_FormErrors
       
   118      */
       
   119     public function setElementSeparator($string)
       
   120     {
       
   121         $this->_htmlElementSeparator = (string) $string;
       
   122         return $this;
       
   123     }
       
   124 
       
   125     /**
       
   126      * Retrieve separator string for displaying errors
       
   127      *
       
   128      * @return string
       
   129      */
       
   130     public function getElementSeparator()
       
   131     {
       
   132         return $this->_htmlElementSeparator;
       
   133     }
       
   134 
       
   135     /**
       
   136      * Set start string for displaying errors
       
   137      *
       
   138      * @param  string $string
       
   139      * @return Zend_View_Helper_FormErrors
       
   140      */
       
   141     public function setElementStart($string)
       
   142     {
       
   143         $this->_htmlElementStart = (string) $string;
       
   144         return $this;
       
   145     }
       
   146 
       
   147     /**
       
   148      * Retrieve start string for displaying errors
       
   149      *
       
   150      * @return string
       
   151      */
       
   152     public function getElementStart()
       
   153     {
       
   154         return $this->_htmlElementStart;
       
   155     }
       
   156 
       
   157 }