[BUGFIX] Submitted form data has precedence over value argument
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Form / CheckboxViewHelper.php
1 <?php
2
3 namespace TYPO3\CMS\Fluid\ViewHelpers\Form;
4
5 /* *
6 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
7 * *
8 * It is free software; you can redistribute it and/or modify it under *
9 * the terms of the GNU Lesser General Public License, either version 3 *
10 * of the License, or (at your option) any later version. *
11 * *
12 * The TYPO3 project - inspiring people to share! *
13 * */
14
15 /**
16 * View Helper which creates a simple checkbox (<input type="checkbox">).
17 *
18 * = Examples =
19 *
20 * <code title="Example">
21 * <f:form.checkbox name="myCheckBox" value="someValue" />
22 * </code>
23 * <output>
24 * <input type="checkbox" name="myCheckBox" value="someValue" />
25 * </output>
26 *
27 * <code title="Preselect">
28 * <f:form.checkbox name="myCheckBox" value="someValue" checked="{object.value} == 5" />
29 * </code>
30 * <output>
31 * <input type="checkbox" name="myCheckBox" value="someValue" checked="checked" />
32 * (depending on $object)
33 * </output>
34 *
35 * <code title="Bind to object property">
36 * <f:form.checkbox property="interests" value="TYPO3" />
37 * </code>
38 * <output>
39 * <input type="checkbox" name="user[interests][]" value="TYPO3" checked="checked" />
40 * (depending on property "interests")
41 * </output>
42 *
43 * @api
44 */
45 class CheckboxViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper {
46
47 /**
48 * @var string
49 */
50 protected $tagName = 'input';
51
52 /**
53 * Initialize the arguments.
54 *
55 * @return void
56 * @api
57 */
58 public function initializeArguments() {
59 parent::initializeArguments();
60 $this->registerTagAttribute(
61 'disabled', 'string', 'Specifies that the input element should be disabled when the page loads'
62 );
63 $this->registerArgument(
64 'errorClass', 'string', 'CSS class to set if there are errors for this view helper', FALSE, 'f3-form-error'
65 );
66 $this->overrideArgument('value', 'string', 'Value of input tag. Required for checkboxes', TRUE);
67 $this->registerUniversalTagAttributes();
68 }
69
70 /**
71 * Renders the checkbox.
72 *
73 * @param bool $checked Specifies that the input element should be preselected
74 * @param bool $multiple Specifies whether this checkbox belongs to a multivalue (is part of a checkbox group)
75 * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
76 * @return string
77 * @api
78 */
79 public function render($checked = NULL, $multiple = NULL) {
80 $this->tag->addAttribute('type', 'checkbox');
81
82 $nameAttribute = $this->getName();
83 $valueAttribute = $this->getValueAttribute();
84 $propertyValue = NULL;
85 if ($this->hasMappingErrorOccurred()) {
86 $propertyValue = $this->getLastSubmittedFormData();
87 }
88 if ($checked === NULL && $propertyValue === NULL) {
89 $propertyValue = $this->getPropertyValue();
90 }
91
92 if ($propertyValue instanceof \Traversable) {
93 $propertyValue = iterator_to_array($propertyValue);
94 }
95 if (is_array($propertyValue)) {
96 $propertyValue = array_map(array($this, 'convertToPlainValue'), $propertyValue);
97 if ($checked === NULL) {
98 $checked = in_array($valueAttribute, $propertyValue);
99 }
100 $nameAttribute .= '[]';
101 } elseif ($multiple === TRUE) {
102 $nameAttribute .= '[]';
103 } elseif ($propertyValue !== NULL) {
104 $checked = (boolean) $propertyValue === (boolean) $valueAttribute;
105 }
106
107 $this->registerFieldNameForFormTokenGeneration($nameAttribute);
108 $this->tag->addAttribute('name', $nameAttribute);
109 $this->tag->addAttribute('value', $valueAttribute);
110 if ($checked === TRUE) {
111 $this->tag->addAttribute('checked', 'checked');
112 }
113
114 $this->setErrorClassAttribute();
115 $hiddenField = $this->renderHiddenFieldForEmptyValue();
116 return $hiddenField . $this->tag->render();
117 }
118
119 }