[FEATURE] Added autofocus attribute to textfield and textarea
[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 a 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->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this view helper', FALSE, 'f3-form-error');
50 $this->registerUniversalTagAttributes();
51 }
52
53 /**
54 * Renders the textfield.
55 *
56 * @param boolean $required If the field is required or not
57 * @param string $type The field type, e.g. "text", "email", "url" etc.
58 * @return string
59 * @api
60 */
61 public function render($required = NULL, $type = 'text') {
62 $name = $this->getName();
63 $this->registerFieldNameForFormTokenGeneration($name);
64
65 $this->tag->addAttribute('type', $type);
66 $this->tag->addAttribute('name', $name);
67
68 $value = $this->getValue();
69
70 if ($value !== NULL) {
71 $this->tag->addAttribute('value', $value);
72 }
73
74 if ($required !== NULL) {
75 $this->tag->addAttribute('required', 'required');
76 }
77
78 $this->setErrorClassAttribute();
79
80 return $this->tag->render();
81 }
82 }
83
84 ?>