/library/Zend/Form/View/Helper/FormRow.php

https://bitbucket.org/saifshuvo/zf2 · PHP · 393 lines · 211 code · 53 blank · 129 comment · 35 complexity · d23b4f6c8f7aafcfe016586b7d20dd96 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Form\View\Helper;
  10. use Zend\Form\ElementInterface;
  11. use Zend\Form\Exception;
  12. use Zend\Form\View\Helper\AbstractHelper;
  13. class FormRow extends AbstractHelper
  14. {
  15. const LABEL_APPEND = 'append';
  16. const LABEL_PREPEND = 'prepend';
  17. /**
  18. * The class that is added to element that have errors
  19. *
  20. * @var string
  21. */
  22. protected $inputErrorClass = 'input-error';
  23. /**
  24. * The attributes for the row label
  25. *
  26. * @var array
  27. */
  28. protected $labelAttributes;
  29. /**
  30. * Where will be label rendered?
  31. *
  32. * @var string
  33. */
  34. protected $labelPosition = self::LABEL_PREPEND;
  35. /**
  36. * Are the errors are rendered by this helper?
  37. *
  38. * @var bool
  39. */
  40. protected $renderErrors = true;
  41. /**
  42. * Form label helper instance
  43. *
  44. * @var FormLabel
  45. */
  46. protected $labelHelper;
  47. /**
  48. * Form element helper instance
  49. *
  50. * @var FormElement
  51. */
  52. protected $elementHelper;
  53. /**
  54. * Form element errors helper instance
  55. *
  56. * @var FormElementErrors
  57. */
  58. protected $elementErrorsHelper;
  59. /**
  60. * @var string
  61. */
  62. protected $partial;
  63. /**
  64. * @var array
  65. */
  66. protected $partialVars = array();
  67. /**
  68. * Invoke helper as functor
  69. *
  70. * Proxies to {@link render()}.
  71. *
  72. * @param null|ElementInterface $element
  73. * @param null|string $labelPosition
  74. * @param bool $renderErrors
  75. * @return string|FormRow
  76. */
  77. public function __invoke(ElementInterface $element = null, $labelPosition = null, $renderErrors = null)
  78. {
  79. if (!$element) {
  80. return $this;
  81. }
  82. if ($labelPosition !== null) {
  83. $this->setLabelPosition($labelPosition);
  84. } else {
  85. $this->setLabelPosition(self::LABEL_PREPEND);
  86. }
  87. if ($renderErrors !== null) {
  88. $this->setRenderErrors($renderErrors);
  89. }
  90. return $this->render($element);
  91. }
  92. /**
  93. * Utility form helper that renders a label (if it exists), an element and errors
  94. *
  95. * @param ElementInterface $element
  96. * @throws \Zend\Form\Exception\DomainException
  97. * @return string
  98. */
  99. public function render(ElementInterface $element)
  100. {
  101. $escapeHtmlHelper = $this->getEscapeHtmlHelper();
  102. $labelHelper = $this->getLabelHelper();
  103. $elementHelper = $this->getElementHelper();
  104. $elementErrorsHelper = $this->getElementErrorsHelper();
  105. $label = $element->getLabel();
  106. $inputErrorClass = $this->getInputErrorClass();
  107. $elementErrors = $elementErrorsHelper->render($element);
  108. // Does this element have errors ?
  109. if (!empty($elementErrors) && !empty($inputErrorClass)) {
  110. $classAttributes = ($element->hasAttribute('class') ? $element->getAttribute('class') . ' ' : '');
  111. $classAttributes = $classAttributes . $inputErrorClass;
  112. $element->setAttribute('class', $classAttributes);
  113. }
  114. $elementString = $elementHelper->render($element);
  115. if (isset($label) && '' !== $label) {
  116. // Translate the label
  117. if (null !== ($translator = $this->getTranslator())) {
  118. $label = $translator->translate(
  119. $label, $this->getTranslatorTextDomain()
  120. );
  121. }
  122. $label = $escapeHtmlHelper($label);
  123. $labelAttributes = $element->getLabelAttributes();
  124. if (empty($labelAttributes)) {
  125. $labelAttributes = $this->labelAttributes;
  126. }
  127. // Multicheckbox elements have to be handled differently as the HTML standard does not allow nested
  128. // labels. The semantic way is to group them inside a fieldset
  129. $type = $element->getAttribute('type');
  130. if ($type === 'multi_checkbox' || $type === 'radio') {
  131. $markup = sprintf(
  132. '<fieldset><legend>%s</legend>%s</fieldset>',
  133. $label,
  134. $elementString);
  135. } else {
  136. if ($element->hasAttribute('id')) {
  137. $labelOpen = '';
  138. $labelClose = '';
  139. $label = $labelHelper($element);
  140. } else {
  141. $labelOpen = $labelHelper->openTag($labelAttributes);
  142. $labelClose = $labelHelper->closeTag();
  143. }
  144. if ($label !== '' && !$element->hasAttribute('id')) {
  145. $label = '<span>' . $label . '</span>';
  146. }
  147. if ($this->partial) {
  148. $vars = array(
  149. 'element' => $elementString,
  150. 'label' => $label,
  151. 'labelOpen' => $labelOpen,
  152. 'labelClose' => $labelClose,
  153. 'labelPosition' => $this->labelPosition,
  154. 'errors' => $elementErrors,
  155. 'renderErrors' => $this->renderErrors
  156. );
  157. return $this->view->render($this->partial, array_merge($vars, $this->partialVars));
  158. }
  159. switch ($this->labelPosition) {
  160. case self::LABEL_PREPEND:
  161. $markup = $labelOpen . $label . $elementString . $labelClose;
  162. break;
  163. case self::LABEL_APPEND:
  164. default:
  165. $markup = $labelOpen . $elementString . $label . $labelClose;
  166. break;
  167. }
  168. }
  169. if ($this->renderErrors) {
  170. $markup .= $elementErrors;
  171. }
  172. } else {
  173. if ($this->partial) {
  174. $vars = array(
  175. 'element' => $elementString,
  176. 'errors' => $elementErrors,
  177. 'renderErrors' => $this->renderErrors
  178. );
  179. return $this->view->render($this->partial, array_merge($vars, $this->partialVars));
  180. }
  181. if ($this->renderErrors) {
  182. $markup = $elementString . $elementErrors;
  183. } else {
  184. $markup = $elementString;
  185. }
  186. }
  187. return $markup;
  188. }
  189. /**
  190. * Set the class that is added to element that have errors
  191. *
  192. * @param string $inputErrorClass
  193. * @return FormRow
  194. */
  195. public function setInputErrorClass($inputErrorClass)
  196. {
  197. $this->inputErrorClass = $inputErrorClass;
  198. return $this;
  199. }
  200. /**
  201. * Get the class that is added to element that have errors
  202. *
  203. * @return string
  204. */
  205. public function getInputErrorClass()
  206. {
  207. return $this->inputErrorClass;
  208. }
  209. /**
  210. * Set the attributes for the row label
  211. *
  212. * @param array $labelAttributes
  213. * @return FormRow
  214. */
  215. public function setLabelAttributes($labelAttributes)
  216. {
  217. $this->labelAttributes = $labelAttributes;
  218. return $this;
  219. }
  220. /**
  221. * Get the attributes for the row label
  222. *
  223. * @return array
  224. */
  225. public function getLabelAttributes()
  226. {
  227. return $this->labelAttributes;
  228. }
  229. /**
  230. * Set the label position
  231. *
  232. * @param string $labelPosition
  233. * @throws \Zend\Form\Exception\InvalidArgumentException
  234. * @return FormRow
  235. */
  236. public function setLabelPosition($labelPosition)
  237. {
  238. $labelPosition = strtolower($labelPosition);
  239. if (!in_array($labelPosition, array(self::LABEL_APPEND, self::LABEL_PREPEND))) {
  240. throw new Exception\InvalidArgumentException(sprintf(
  241. '%s expects either %s::LABEL_APPEND or %s::LABEL_PREPEND; received "%s"',
  242. __METHOD__,
  243. __CLASS__,
  244. __CLASS__,
  245. (string) $labelPosition
  246. ));
  247. }
  248. $this->labelPosition = $labelPosition;
  249. return $this;
  250. }
  251. /**
  252. * Get the label position
  253. *
  254. * @return string
  255. */
  256. public function getLabelPosition()
  257. {
  258. return $this->labelPosition;
  259. }
  260. /**
  261. * Set if the errors are rendered by this helper
  262. *
  263. * @param bool $renderErrors
  264. * @return FormRow
  265. */
  266. public function setRenderErrors($renderErrors)
  267. {
  268. $this->renderErrors = (bool) $renderErrors;
  269. return $this;
  270. }
  271. /**
  272. * Retrive if the errors are rendered by this helper
  273. *
  274. * @return bool
  275. */
  276. public function getRenderErrors()
  277. {
  278. return $this->renderErrors;
  279. }
  280. /**
  281. * Retrieve the FormLabel helper
  282. *
  283. * @return FormLabel
  284. */
  285. protected function getLabelHelper()
  286. {
  287. if ($this->labelHelper) {
  288. return $this->labelHelper;
  289. }
  290. if (method_exists($this->view, 'plugin')) {
  291. $this->labelHelper = $this->view->plugin('form_label');
  292. }
  293. if (!$this->labelHelper instanceof FormLabel) {
  294. $this->labelHelper = new FormLabel();
  295. }
  296. if ($this->hasTranslator()) {
  297. $this->labelHelper->setTranslator(
  298. $this->getTranslator(),
  299. $this->getTranslatorTextDomain()
  300. );
  301. }
  302. return $this->labelHelper;
  303. }
  304. /**
  305. * Retrieve the FormElement helper
  306. *
  307. * @return FormElement
  308. */
  309. protected function getElementHelper()
  310. {
  311. if ($this->elementHelper) {
  312. return $this->elementHelper;
  313. }
  314. if (method_exists($this->view, 'plugin')) {
  315. $this->elementHelper = $this->view->plugin('form_element');
  316. }
  317. if (!$this->elementHelper instanceof FormElement) {
  318. $this->elementHelper = new FormElement();
  319. }
  320. return $this->elementHelper;
  321. }
  322. /**
  323. * Retrieve the FormElementErrors helper
  324. *
  325. * @return FormElementErrors
  326. */
  327. protected function getElementErrorsHelper()
  328. {
  329. if ($this->elementErrorsHelper) {
  330. return $this->elementErrorsHelper;
  331. }
  332. if (method_exists($this->view, 'plugin')) {
  333. $this->elementErrorsHelper = $this->view->plugin('form_element_errors');
  334. }
  335. if (!$this->elementErrorsHelper instanceof FormElementErrors) {
  336. $this->elementErrorsHelper = new FormElementErrors();
  337. }
  338. return $this->elementErrorsHelper;
  339. }
  340. }