PageRenderTime 25ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Zend/Form/Decorator/FormErrors.php

https://github.com/albertobraschi/NFS
PHP | 397 lines | 215 code | 36 blank | 146 comment | 33 complexity | 5ae1054168e72fa08a455ad8b1ede0e2 MD5 | raw file
  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_Form
  17. * @subpackage Decorator
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Form_Decorator_Abstract */
  22. #require_once 'Zend/Form/Decorator/Abstract.php';
  23. /**
  24. * Zend_Form_Decorator_FormErrors
  25. *
  26. * Displays all form errors in one view.
  27. *
  28. * Any options passed will be used as HTML attributes of the ul tag for the errors.
  29. *
  30. * @category Zend
  31. * @package Zend_Form
  32. * @subpackage Decorator
  33. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @version $Id$
  36. */
  37. class Zend_Form_Decorator_FormErrors extends Zend_Form_Decorator_Abstract
  38. {
  39. /**
  40. * Default values for markup options
  41. * @var array
  42. */
  43. protected $_defaults = array(
  44. 'ignoreSubForms' => false,
  45. 'markupElementLabelEnd' => '</b>',
  46. 'markupElementLabelStart' => '<b>',
  47. 'markupListEnd' => '</ul>',
  48. 'markupListItemEnd' => '</li>',
  49. 'markupListItemStart' => '<li>',
  50. 'markupListStart' => '<ul class="form-errors">',
  51. );
  52. /**#@+
  53. * Markup options
  54. * @var string
  55. */
  56. protected $_ignoreSubForms;
  57. protected $_markupElementLabelEnd;
  58. protected $_markupElementLabelStart;
  59. protected $_markupListEnd;
  60. protected $_markupListItemEnd;
  61. protected $_markupListItemStart;
  62. protected $_markupListStart;
  63. /**#@-*/
  64. /**
  65. * Render errors
  66. *
  67. * @param string $content
  68. * @return string
  69. */
  70. public function render($content)
  71. {
  72. $form = $this->getElement();
  73. if (!$form instanceof Zend_Form) {
  74. return $content;
  75. }
  76. $view = $form->getView();
  77. if (null === $view) {
  78. return $content;
  79. }
  80. $this->initOptions();
  81. $markup = $this->_recurseForm($form, $view);
  82. if (empty($markup)) {
  83. return $content;
  84. }
  85. $markup = $this->getMarkupListStart()
  86. . $markup
  87. . $this->getMarkupListEnd();
  88. switch ($this->getPlacement()) {
  89. case self::APPEND:
  90. return $content . $this->getSeparator() . $markup;
  91. case self::PREPEND:
  92. return $markup . $this->getSeparator() . $content;
  93. }
  94. }
  95. /**
  96. * Initialize options
  97. *
  98. * @return void
  99. */
  100. public function initOptions()
  101. {
  102. $this->getMarkupElementLabelEnd();
  103. $this->getMarkupElementLabelStart();
  104. $this->getMarkupListEnd();
  105. $this->getMarkupListItemEnd();
  106. $this->getMarkupListItemStart();
  107. $this->getMarkupListStart();
  108. $this->getPlacement();
  109. $this->getSeparator();
  110. $this->ignoreSubForms();
  111. }
  112. /**
  113. * Retrieve markupElementLabelStart
  114. *
  115. * @return string
  116. */
  117. public function getMarkupElementLabelStart()
  118. {
  119. if (null === $this->_markupElementLabelStart) {
  120. if (null === ($markupElementLabelStart = $this->getOption('markupElementLabelStart'))) {
  121. $this->setMarkupElementLabelStart($this->_defaults['markupElementLabelStart']);
  122. } else {
  123. $this->setMarkupElementLabelStart($markupElementLabelStart);
  124. $this->removeOption('markupElementLabelStart');
  125. }
  126. }
  127. return $this->_markupElementLabelStart;
  128. }
  129. /**
  130. * Set markupElementLabelStart
  131. *
  132. * @param string $markupElementLabelStart
  133. * @return Zend_Form_Decorator_FormErrors
  134. */
  135. public function setMarkupElementLabelStart($markupElementLabelStart)
  136. {
  137. $this->_markupElementLabelStart = $markupElementLabelStart;
  138. return $this;
  139. }
  140. /**
  141. * Retrieve markupElementLabelEnd
  142. *
  143. * @return string
  144. */
  145. public function getMarkupElementLabelEnd()
  146. {
  147. if (null === $this->_markupElementLabelEnd) {
  148. if (null === ($markupElementLabelEnd = $this->getOption('markupElementLabelEnd'))) {
  149. $this->setMarkupElementLabelEnd($this->_defaults['markupElementLabelEnd']);
  150. } else {
  151. $this->setMarkupElementLabelEnd($markupElementLabelEnd);
  152. $this->removeOption('markupElementLabelEnd');
  153. }
  154. }
  155. return $this->_markupElementLabelEnd;
  156. }
  157. /**
  158. * Set markupElementLabelEnd
  159. *
  160. * @param string $markupElementLabelEnd
  161. * @return Zend_Form_Decorator_FormErrors
  162. */
  163. public function setMarkupElementLabelEnd($markupElementLabelEnd)
  164. {
  165. $this->_markupElementLabelEnd = $markupElementLabelEnd;
  166. return $this;
  167. }
  168. /**
  169. * Retrieve markupListStart
  170. *
  171. * @return string
  172. */
  173. public function getMarkupListStart()
  174. {
  175. if (null === $this->_markupListStart) {
  176. if (null === ($markupListStart = $this->getOption('markupListStart'))) {
  177. $this->setMarkupListStart($this->_defaults['markupListStart']);
  178. } else {
  179. $this->setMarkupListStart($markupListStart);
  180. $this->removeOption('markupListStart');
  181. }
  182. }
  183. return $this->_markupListStart;
  184. }
  185. /**
  186. * Set markupListStart
  187. *
  188. * @param string $markupListStart
  189. * @return Zend_Form_Decorator_FormErrors
  190. */
  191. public function setMarkupListStart($markupListStart)
  192. {
  193. $this->_markupListStart = $markupListStart;
  194. return $this;
  195. }
  196. /**
  197. * Retrieve markupListEnd
  198. *
  199. * @return string
  200. */
  201. public function getMarkupListEnd()
  202. {
  203. if (null === $this->_markupListEnd) {
  204. if (null === ($markupListEnd = $this->getOption('markupListEnd'))) {
  205. $this->setMarkupListEnd($this->_defaults['markupListEnd']);
  206. } else {
  207. $this->setMarkupListEnd($markupListEnd);
  208. $this->removeOption('markupListEnd');
  209. }
  210. }
  211. return $this->_markupListEnd;
  212. }
  213. /**
  214. * Set markupListEnd
  215. *
  216. * @param string $markupListEnd
  217. * @return Zend_Form_Decorator_FormErrors
  218. */
  219. public function setMarkupListEnd($markupListEnd)
  220. {
  221. $this->_markupListEnd = $markupListEnd;
  222. return $this;
  223. }
  224. /**
  225. * Retrieve markupListItemStart
  226. *
  227. * @return string
  228. */
  229. public function getMarkupListItemStart()
  230. {
  231. if (null === $this->_markupListItemStart) {
  232. if (null === ($markupListItemStart = $this->getOption('markupListItemStart'))) {
  233. $this->setMarkupListItemStart($this->_defaults['markupListItemStart']);
  234. } else {
  235. $this->setMarkupListItemStart($markupListItemStart);
  236. $this->removeOption('markupListItemStart');
  237. }
  238. }
  239. return $this->_markupListItemStart;
  240. }
  241. /**
  242. * Set markupListItemStart
  243. *
  244. * @param string $markupListItemStart
  245. * @return Zend_Form_Decorator_FormErrors
  246. */
  247. public function setMarkupListItemStart($markupListItemStart)
  248. {
  249. $this->_markupListItemStart = $markupListItemStart;
  250. return $this;
  251. }
  252. /**
  253. * Retrieve markupListItemEnd
  254. *
  255. * @return string
  256. */
  257. public function getMarkupListItemEnd()
  258. {
  259. if (null === $this->_markupListItemEnd) {
  260. if (null === ($markupListItemEnd = $this->getOption('markupListItemEnd'))) {
  261. $this->setMarkupListItemEnd($this->_defaults['markupListItemEnd']);
  262. } else {
  263. $this->setMarkupListItemEnd($markupListItemEnd);
  264. $this->removeOption('markupListItemEnd');
  265. }
  266. }
  267. return $this->_markupListItemEnd;
  268. }
  269. /**
  270. * Set markupListItemEnd
  271. *
  272. * @param string $markupListItemEnd
  273. * @return Zend_Form_Decorator_FormErrors
  274. */
  275. public function setMarkupListItemEnd($markupListItemEnd)
  276. {
  277. $this->_markupListItemEnd = $markupListItemEnd;
  278. return $this;
  279. }
  280. /**
  281. * Retrieve ignoreSubForms
  282. *
  283. * @return bool
  284. */
  285. public function ignoreSubForms()
  286. {
  287. if (null === $this->_ignoreSubForms) {
  288. if (null === ($ignoreSubForms = $this->getOption('ignoreSubForms'))) {
  289. $this->setIgnoreSubForms($this->_defaults['ignoreSubForms']);
  290. } else {
  291. $this->setIgnoreSubForms($ignoreSubForms);
  292. $this->removeOption('ignoreSubForms');
  293. }
  294. }
  295. return $this->_ignoreSubForms;
  296. }
  297. /**
  298. * Set ignoreSubForms
  299. *
  300. * @param bool $ignoreSubForms
  301. * @return Zend_Form_Decorator_FormErrors
  302. */
  303. public function setIgnoreSubForms($ignoreSubForms)
  304. {
  305. $this->_ignoreSubForms = (bool) $ignoreSubForms;
  306. return $this;
  307. }
  308. /**
  309. * Render element label
  310. *
  311. * @param Zend_Form_Element $element
  312. * @param Zend_View_Interface $view
  313. * @return string
  314. */
  315. public function renderLabel(Zend_Form_Element $element, Zend_View_Interface $view)
  316. {
  317. $label = $element->getLabel();
  318. if (empty($label)) {
  319. $label = $element->getName();
  320. }
  321. return $this->getMarkupElementLabelStart()
  322. . $view->escape($label)
  323. . $this->getMarkupElementLabelEnd();
  324. }
  325. /**
  326. * Recurse through a form object, rendering errors
  327. *
  328. * @param Zend_Form $form
  329. * @param Zend_View_Interface $view
  330. * @return string
  331. */
  332. protected function _recurseForm(Zend_Form $form, Zend_View_Interface $view)
  333. {
  334. $content = '';
  335. $errors = $form->getMessages();
  336. if ($form instanceof Zend_Form_SubForm) {
  337. $name = $form->getName();
  338. if ((1 == count($errors)) && array_key_exists($name, $errors)) {
  339. $errors = $errors[$name];
  340. }
  341. }
  342. if (empty($errors)) {
  343. return $content;
  344. }
  345. foreach ($errors as $name => $list) {
  346. $element = $form->$name;
  347. if ($element instanceof Zend_Form_Element) {
  348. $element->setView($view);
  349. $content .= $this->getMarkupListItemStart()
  350. . $this->renderLabel($element, $view)
  351. . $view->formErrors($list, $this->getOptions())
  352. . $this->getMarkupListItemEnd();
  353. } elseif (!$this->ignoreSubForms() && ($element instanceof Zend_Form)) {
  354. $content .= $this->getMarkupListStart()
  355. . $this->_recurseForm($element, $view)
  356. . $this->getMarkupListEnd();
  357. }
  358. }
  359. return $content;
  360. }
  361. }