[BUGFIX] Submitted form data has precedence over value argument
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Form / TextfieldViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Form;
3
4 /* *
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13
14 /**
15 * View Helper which creates a text field (<input type="text">).
16 *
17 * = Examples =
18 *
19 * <code title="Example">
20 * <f:form.textfield name="myTextBox" value="default value" />
21 * </code>
22 * <output>
23 * <input type="text" name="myTextBox" value="default value" />
24 * </output>
25 *
26 * @api
27 */
28 class TextfieldViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper {
29
30 /**
31 * @var string
32 */
33 protected $tagName = 'input';
34
35 /**
36 * Initialize the arguments.
37 *
38 * @return void
39 * @api
40 */
41 public function initializeArguments() {
42 parent::initializeArguments();
43 $this->registerTagAttribute('autofocus', 'string', 'Specifies that an input should automatically get focus when the page loads');
44 $this->registerTagAttribute('disabled', 'string', 'Specifies that the input element should be disabled when the page loads');
45 $this->registerTagAttribute('maxlength', 'int', 'The maxlength attribute of the input field (will not be validated)');
46 $this->registerTagAttribute('readonly', 'string', 'The readonly attribute of the input field');
47 $this->registerTagAttribute('size', 'int', 'The size of the input field');
48 $this->registerTagAttribute('placeholder', 'string', 'The placeholder of the textfield');
49 $this->registerTagAttribute('pattern', 'string', 'HTML5 validation pattern');
50 $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this view helper', FALSE, 'f3-form-error');
51 $this->registerUniversalTagAttributes();
52 }
53
54 /**
55 * Renders the textfield.
56 *
57 * @param bool $required If the field is required or not
58 * @param string $type The field type, e.g. "text", "email", "url" etc.
59 * @return string
60 * @api
61 */
62 public function render($required = NULL, $type = 'text') {
63 $name = $this->getName();
64 $this->registerFieldNameForFormTokenGeneration($name);
65 $this->setRespectSubmittedDataValue(TRUE);
66
67 $this->tag->addAttribute('type', $type);
68 $this->tag->addAttribute('name', $name);
69
70 $value = $this->getValueAttribute();
71
72 if ($value !== NULL) {
73 $this->tag->addAttribute('value', $value);
74 }
75
76 if ($required !== NULL) {
77 $this->tag->addAttribute('required', 'required');
78 }
79
80 $this->addAdditionalIdentityPropertiesIfNeeded();
81 $this->setErrorClassAttribute();
82
83 return $this->tag->render();
84 }
85
86 }