FormErrors.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Abstract class for extension
  24. */
  25. require_once 'Zend/View/Helper/FormElement.php';
  26. /**
  27. * Helper to render errors for a form element
  28. *
  29. * @category Zend
  30. * @package Zend_View
  31. * @subpackage Helper
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_View_Helper_FormErrors extends Zend_View_Helper_FormElement
  36. {
  37. /**
  38. * @var Zend_Form_Element
  39. */
  40. protected $_element;
  41. /**#@+
  42. * @var string Element block start/end tags and separator
  43. */
  44. protected $_htmlElementEnd = '</li></ul>';
  45. protected $_htmlElementStart = '<ul%s><li>';
  46. protected $_htmlElementSeparator = '</li><li>';
  47. /**#@-*/
  48. /**
  49. * Render form errors
  50. *
  51. * @param string|array $errors Error(s) to render
  52. * @param array $options
  53. * @return string
  54. */
  55. public function formErrors($errors, array $options = null)
  56. {
  57. $escape = true;
  58. if (isset($options['escape'])) {
  59. $escape = (bool) $options['escape'];
  60. unset($options['escape']);
  61. }
  62. if (empty($options['class'])) {
  63. $options['class'] = 'errors';
  64. }
  65. if (isset($options['elementStart'])) {
  66. $this->setElementStart($options['elementStart']);
  67. }
  68. if (isset($options['elementEnd'])) {
  69. $this->setElementEnd($options['elementEnd']);
  70. }
  71. if (isset($options['elementSeparator'])) {
  72. $this->setElementSeparator($options['elementSeparator']);
  73. }
  74. $start = $this->getElementStart();
  75. if (strstr($start, '%s')) {
  76. $attribs = $this->_htmlAttribs($options);
  77. $start = sprintf($start, $attribs);
  78. }
  79. if ($escape) {
  80. foreach ($errors as $key => $error) {
  81. $errors[$key] = $this->view->escape($error);
  82. }
  83. }
  84. $html = $start
  85. . implode($this->getElementSeparator(), (array) $errors)
  86. . $this->getElementEnd();
  87. return $html;
  88. }
  89. /**
  90. * Set end string for displaying errors
  91. *
  92. * @param string $string
  93. * @return Zend_View_Helper_FormErrors
  94. */
  95. public function setElementEnd($string)
  96. {
  97. $this->_htmlElementEnd = (string) $string;
  98. return $this;
  99. }
  100. /**
  101. * Retrieve end string for displaying errors
  102. *
  103. * @return string
  104. */
  105. public function getElementEnd()
  106. {
  107. return $this->_htmlElementEnd;
  108. }
  109. /**
  110. * Set separator string for displaying errors
  111. *
  112. * @param string $string
  113. * @return Zend_View_Helper_FormErrors
  114. */
  115. public function setElementSeparator($string)
  116. {
  117. $this->_htmlElementSeparator = (string) $string;
  118. return $this;
  119. }
  120. /**
  121. * Retrieve separator string for displaying errors
  122. *
  123. * @return string
  124. */
  125. public function getElementSeparator()
  126. {
  127. return $this->_htmlElementSeparator;
  128. }
  129. /**
  130. * Set start string for displaying errors
  131. *
  132. * @param string $string
  133. * @return Zend_View_Helper_FormErrors
  134. */
  135. public function setElementStart($string)
  136. {
  137. $this->_htmlElementStart = (string) $string;
  138. return $this;
  139. }
  140. /**
  141. * Retrieve start string for displaying errors
  142. *
  143. * @return string
  144. */
  145. public function getElementStart()
  146. {
  147. return $this->_htmlElementStart;
  148. }
  149. }