PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Form/Decorator/FormErrors.php

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