composer package updates
[openemr.git] / vendor / zendframework / zend-form / src / View / Helper / FormRow.php
blob95ddbe7f5f44d6c82c861fdfa4813e60dc1a3138
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Form\View\Helper;
12 use Zend\Form\Element\Button;
13 use Zend\Form\Element\MonthSelect;
14 use Zend\Form\Element\Captcha;
15 use Zend\Form\ElementInterface;
16 use Zend\Form\Exception;
17 use Zend\Form\LabelAwareInterface;
19 class FormRow extends AbstractHelper
21 const LABEL_APPEND = 'append';
22 const LABEL_PREPEND = 'prepend';
24 /**
25 * The class that is added to element that have errors
27 * @var string
29 protected $inputErrorClass = 'input-error';
31 /**
32 * The attributes for the row label
34 * @var array
36 protected $labelAttributes;
38 /**
39 * Where will be label rendered?
41 * @var string
43 protected $labelPosition = self::LABEL_PREPEND;
45 /**
46 * Are the errors are rendered by this helper?
48 * @var bool
50 protected $renderErrors = true;
52 /**
53 * Form label helper instance
55 * @var FormLabel
57 protected $labelHelper;
59 /**
60 * Form element helper instance
62 * @var FormElement
64 protected $elementHelper;
66 /**
67 * Form element errors helper instance
69 * @var FormElementErrors
71 protected $elementErrorsHelper;
73 /**
74 * @var string
76 protected $partial;
78 /**
79 * Invoke helper as functor
81 * Proxies to {@link render()}.
83 * @param null|ElementInterface $element
84 * @param null|string $labelPosition
85 * @param bool $renderErrors
86 * @param string|null $partial
87 * @return string|FormRow
89 public function __invoke(
90 ElementInterface $element = null,
91 $labelPosition = null,
92 $renderErrors = null,
93 $partial = null
94 ) {
95 if (! $element) {
96 return $this;
99 if (is_null($labelPosition)) {
100 $labelPosition = $this->getLabelPosition();
103 if ($renderErrors !== null) {
104 $this->setRenderErrors($renderErrors);
107 if ($partial !== null) {
108 $this->setPartial($partial);
111 return $this->render($element, $labelPosition);
115 * Utility form helper that renders a label (if it exists), an element and errors
117 * @param ElementInterface $element
118 * @param null|string $labelPosition
119 * @throws \Zend\Form\Exception\DomainException
120 * @return string
122 public function render(ElementInterface $element, $labelPosition = null)
124 $escapeHtmlHelper = $this->getEscapeHtmlHelper();
125 $labelHelper = $this->getLabelHelper();
126 $elementHelper = $this->getElementHelper();
127 $elementErrorsHelper = $this->getElementErrorsHelper();
129 $label = $element->getLabel();
130 $inputErrorClass = $this->getInputErrorClass();
132 if (is_null($labelPosition)) {
133 $labelPosition = $this->labelPosition;
136 if (isset($label) && '' !== $label) {
137 // Translate the label
138 if (null !== ($translator = $this->getTranslator())) {
139 $label = $translator->translate($label, $this->getTranslatorTextDomain());
143 // Does this element have errors ?
144 if ($element->getMessages() && $inputErrorClass) {
145 $classAttributes = ($element->hasAttribute('class') ? $element->getAttribute('class') . ' ' : '');
146 $classAttributes = $classAttributes . $inputErrorClass;
148 $element->setAttribute('class', $classAttributes);
151 if ($this->partial) {
152 $vars = [
153 'element' => $element,
154 'label' => $label,
155 'labelAttributes' => $this->labelAttributes,
156 'labelPosition' => $labelPosition,
157 'renderErrors' => $this->renderErrors,
160 return $this->view->render($this->partial, $vars);
163 if ($this->renderErrors) {
164 $elementErrors = $elementErrorsHelper->render($element);
167 $elementString = $elementHelper->render($element);
169 // hidden elements do not need a <label> -https://github.com/zendframework/zf2/issues/5607
170 $type = $element->getAttribute('type');
171 if (isset($label) && '' !== $label && $type !== 'hidden') {
172 $labelAttributes = [];
174 if ($element instanceof LabelAwareInterface) {
175 $labelAttributes = $element->getLabelAttributes();
178 if (! $element instanceof LabelAwareInterface || ! $element->getLabelOption('disable_html_escape')) {
179 $label = $escapeHtmlHelper($label);
182 if (empty($labelAttributes)) {
183 $labelAttributes = $this->labelAttributes;
186 // Multicheckbox elements have to be handled differently as the HTML standard does not allow nested
187 // labels. The semantic way is to group them inside a fieldset
188 if ($type === 'multi_checkbox'
189 || $type === 'radio'
190 || $element instanceof MonthSelect
191 || $element instanceof Captcha
193 $markup = sprintf(
194 '<fieldset><legend>%s</legend>%s</fieldset>',
195 $label,
196 $elementString
198 } else {
199 // Ensure element and label will be separated if element has an `id`-attribute.
200 // If element has label option `always_wrap` it will be nested in any case.
201 if ($element->hasAttribute('id')
202 && ($element instanceof LabelAwareInterface && ! $element->getLabelOption('always_wrap'))
204 $labelOpen = '';
205 $labelClose = '';
206 $label = $labelHelper->openTag($element) . $label . $labelHelper->closeTag();
207 } else {
208 $labelOpen = $labelHelper->openTag($labelAttributes);
209 $labelClose = $labelHelper->closeTag();
212 if ($label !== '' && (! $element->hasAttribute('id'))
213 || ($element instanceof LabelAwareInterface && $element->getLabelOption('always_wrap'))
215 $label = '<span>' . $label . '</span>';
218 // Button element is a special case, because label is always rendered inside it
219 if ($element instanceof Button) {
220 $labelOpen = $labelClose = $label = '';
223 if ($element instanceof LabelAwareInterface && $element->getLabelOption('label_position')) {
224 $labelPosition = $element->getLabelOption('label_position');
227 switch ($labelPosition) {
228 case self::LABEL_PREPEND:
229 $markup = $labelOpen . $label . $elementString . $labelClose;
230 break;
231 case self::LABEL_APPEND:
232 default:
233 $markup = $labelOpen . $elementString . $label . $labelClose;
234 break;
238 if ($this->renderErrors) {
239 $markup .= $elementErrors;
241 } else {
242 if ($this->renderErrors) {
243 $markup = $elementString . $elementErrors;
244 } else {
245 $markup = $elementString;
249 return $markup;
253 * Set the class that is added to element that have errors
255 * @param string $inputErrorClass
256 * @return FormRow
258 public function setInputErrorClass($inputErrorClass)
260 $this->inputErrorClass = $inputErrorClass;
261 return $this;
265 * Get the class that is added to element that have errors
267 * @return string
269 public function getInputErrorClass()
271 return $this->inputErrorClass;
275 * Set the attributes for the row label
277 * @param array $labelAttributes
278 * @return FormRow
280 public function setLabelAttributes($labelAttributes)
282 $this->labelAttributes = $labelAttributes;
283 return $this;
287 * Get the attributes for the row label
289 * @return array
291 public function getLabelAttributes()
293 return $this->labelAttributes;
297 * Set the label position
299 * @param string $labelPosition
300 * @throws \Zend\Form\Exception\InvalidArgumentException
301 * @return FormRow
303 public function setLabelPosition($labelPosition)
305 $labelPosition = strtolower($labelPosition);
306 if (! in_array($labelPosition, [self::LABEL_APPEND, self::LABEL_PREPEND])) {
307 throw new Exception\InvalidArgumentException(sprintf(
308 '%s expects either %s::LABEL_APPEND or %s::LABEL_PREPEND; received "%s"',
309 __METHOD__,
310 __CLASS__,
311 __CLASS__,
312 (string) $labelPosition
315 $this->labelPosition = $labelPosition;
317 return $this;
321 * Get the label position
323 * @return string
325 public function getLabelPosition()
327 return $this->labelPosition;
331 * Set if the errors are rendered by this helper
333 * @param bool $renderErrors
334 * @return FormRow
336 public function setRenderErrors($renderErrors)
338 $this->renderErrors = (bool) $renderErrors;
339 return $this;
343 * Retrieve if the errors are rendered by this helper
345 * @return bool
347 public function getRenderErrors()
349 return $this->renderErrors;
353 * Set a partial view script to use for rendering the row
355 * @param null|string $partial
356 * @return FormRow
358 public function setPartial($partial)
360 $this->partial = $partial;
361 return $this;
365 * Retrieve current partial
367 * @return null|string
369 public function getPartial()
371 return $this->partial;
375 * Retrieve the FormLabel helper
377 * @return FormLabel
379 protected function getLabelHelper()
381 if ($this->labelHelper) {
382 return $this->labelHelper;
385 if (method_exists($this->view, 'plugin')) {
386 $this->labelHelper = $this->view->plugin('form_label');
389 if (! $this->labelHelper instanceof FormLabel) {
390 $this->labelHelper = new FormLabel();
393 if ($this->hasTranslator()) {
394 $this->labelHelper->setTranslator(
395 $this->getTranslator(),
396 $this->getTranslatorTextDomain()
400 return $this->labelHelper;
404 * Retrieve the FormElement helper
406 * @return FormElement
408 protected function getElementHelper()
410 if ($this->elementHelper) {
411 return $this->elementHelper;
414 if (method_exists($this->view, 'plugin')) {
415 $this->elementHelper = $this->view->plugin('form_element');
418 if (! $this->elementHelper instanceof FormElement) {
419 $this->elementHelper = new FormElement();
422 return $this->elementHelper;
426 * Retrieve the FormElementErrors helper
428 * @return FormElementErrors
430 protected function getElementErrorsHelper()
432 if ($this->elementErrorsHelper) {
433 return $this->elementErrorsHelper;
436 if (method_exists($this->view, 'plugin')) {
437 $this->elementErrorsHelper = $this->view->plugin('form_element_errors');
440 if (! $this->elementErrorsHelper instanceof FormElementErrors) {
441 $this->elementErrorsHelper = new FormElementErrors();
444 return $this->elementErrorsHelper;