PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/src/application/libraries/Zend/Form/Decorator/FormErrors.php

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