FormErrors.php 3.8 KB

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