[BUGFIX] Submitted form data has precedence over value argument
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Form / RadioViewHelper.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 radio button (<input type="radio">).
17 *
18 * = Examples =
19 *
20 * <code title="Example">
21 * <f:form.radio name="myRadioButton" value="someValue" />
22 * </code>
23 * <output>
24 * <input type="radio" name="myRadioButton" value="someValue" />
25 * </output>
26 *
27 * <code title="Preselect">
28 * <f:form.radio name="myRadioButton" value="someValue" checked="{object.value} == 5" />
29 * </code>
30 * <output>
31 * <input type="radio" name="myRadioButton" value="someValue" checked="checked" />
32 * (depending on $object)
33 * </output>
34 *
35 * <code title="Bind to object property">
36 * <f:form.radio property="newsletter" value="1" /> yes
37 * <f:form.radio property="newsletter" value="0" /> no
38 * </code>
39 * <output>
40 * <input type="radio" name="user[newsletter]" value="1" checked="checked" /> yes
41 * <input type="radio" name="user[newsletter]" value="0" /> no
42 * (depending on property "newsletter")
43 * </output>
44 *
45 * @api
46 */
47 class RadioViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper {
48
49 /**
50 * @var string
51 */
52 protected $tagName = 'input';
53
54 /**
55 * Initialize the arguments.
56 *
57 * @return void
58 * @api
59 */
60 public function initializeArguments() {
61 parent::initializeArguments();
62 $this->registerTagAttribute(
63 'disabled', 'string', 'Specifies that the input element should be disabled when the page loads'
64 );
65 $this->registerArgument(
66 'errorClass', 'string', 'CSS class to set if there are errors for this view helper', FALSE, 'f3-form-error'
67 );
68 $this->overrideArgument('value', 'string', 'Value of input tag. Required for radio buttons', TRUE);
69 $this->registerUniversalTagAttributes();
70 }
71
72 /**
73 * Renders the checkbox.
74 *
75 * @param bool $checked Specifies that the input element should be preselected
76 * @return string
77 * @api
78 */
79 public function render($checked = NULL) {
80 $this->tag->addAttribute('type', 'radio');
81
82 $nameAttribute = $this->getName();
83 $valueAttribute = $this->getValueAttribute();
84
85 $propertyValue = NULL;
86 if ($this->hasMappingErrorOccurred()) {
87 $propertyValue = $this->getLastSubmittedFormData();
88 }
89 if ($checked === NULL && $propertyValue === NULL) {
90 $propertyValue = $this->getPropertyValue();
91 }
92
93 if ($propertyValue !== NULL) {
94
95 // no type-safe comparison by intention
96 $checked = $propertyValue == $valueAttribute;
97 }
98
99 $this->registerFieldNameForFormTokenGeneration($nameAttribute);
100 $this->tag->addAttribute('name', $nameAttribute);
101 $this->tag->addAttribute('value', $valueAttribute);
102 if ($checked === TRUE) {
103 $this->tag->addAttribute('checked', 'checked');
104 }
105
106 $this->setErrorClassAttribute();
107
108 return $this->tag->render();
109 }
110
111 }