2d2a3744b047791b69ff04c60911acbe86b4afb6
[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('disabled', 'string', 'Specifies that the input element should be disabled when the page loads');
44 $this->registerTagAttribute('maxlength', 'int', 'The maxlength attribute of the input field (will not be validated)');
45 $this->registerTagAttribute('readonly', 'string', 'The readonly attribute of the input field');
46 $this->registerTagAttribute('size', 'int', 'The size of the input field');
47 $this->registerTagAttribute('placeholder', 'string', 'The placeholder of the textfield');
48 $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this view helper', FALSE, 'f3-form-error');
49 $this->registerUniversalTagAttributes();
50 }
51
52 /**
53 * Renders the textfield.
54 *
55 * @param boolean $required If the field is required or not
56 * @param string $type The field type, e.g. "text", "email", "url" etc.
57 * @return string
58 * @api
59 */
60 public function render($required = NULL, $type = 'text') {
61 $name = $this->getName();
62 $this->registerFieldNameForFormTokenGeneration($name);
63
64 $this->tag->addAttribute('type', $type);
65 $this->tag->addAttribute('name', $name);
66
67 $value = $this->getValue();
68
69 if ($value !== NULL) {
70 $this->tag->addAttribute('value', $value);
71 }
72
73 if ($required !== NULL) {
74 $this->tag->addAttribute('required', 'required');
75 }
76
77 $this->setErrorClassAttribute();
78
79 return $this->tag->render();
80 }
81 }
82
83 ?>