1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Widget; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Yiisoft\Form\Helper\HtmlForm; |
9
|
|
|
use Yiisoft\Form\Widget\Attribute\GlobalAttributes; |
10
|
|
|
use Yiisoft\Html\Tag\Input; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Generates a text input tag for the given form attribute. |
14
|
|
|
* |
15
|
|
|
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.text.html#input.text |
16
|
|
|
*/ |
17
|
|
|
final class Text extends AbstractWidget |
18
|
|
|
{ |
19
|
|
|
use GlobalAttributes; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Enables submission of a value for the directionality of the element, and gives the name of the field that |
23
|
|
|
* contains that value. |
24
|
|
|
* |
25
|
|
|
* @param string $value Any string that is not empty. |
26
|
|
|
* |
27
|
|
|
* @return static |
28
|
|
|
* |
29
|
|
|
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.text.html#input.text.attrs.dirname |
30
|
|
|
*/ |
31
|
5 |
|
public function dirname(string $value): self |
32
|
|
|
{ |
33
|
5 |
|
if (empty($value)) { |
34
|
2 |
|
throw new InvalidArgumentException('The value cannot be empty.'); |
35
|
|
|
} |
36
|
|
|
|
37
|
3 |
|
$new = clone $this; |
38
|
3 |
|
$new->attributes['dirname'] = $value; |
39
|
3 |
|
return $new; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The maxlength attribute defines the maximum number of characters (as UTF-16 code units) the user can enter into |
44
|
|
|
* a tag input. |
45
|
|
|
* |
46
|
|
|
* If no maxlength is specified, or an invalid value is specified, the tag input has no maximum length. |
47
|
|
|
* |
48
|
|
|
* @param int $value Positive integer. |
49
|
|
|
* |
50
|
|
|
* @return static |
51
|
|
|
* |
52
|
|
|
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.text.html#input.text.attrs.maxlength |
53
|
|
|
*/ |
54
|
3 |
|
public function maxlength(int $value): self |
55
|
|
|
{ |
56
|
3 |
|
$new = clone $this; |
57
|
3 |
|
$new->attributes['maxlength'] = $value; |
58
|
3 |
|
return $new; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* The minimum number of characters (as UTF-16 code units) the user can enter into the text input. |
63
|
|
|
* |
64
|
|
|
* This must be a non-negative integer value smaller than or equal to the value specified by maxlength. |
65
|
|
|
* If no minlength is specified, or an invalid value is specified, the text input has no minimum length. |
66
|
|
|
* |
67
|
|
|
* @param int $value |
68
|
|
|
* |
69
|
|
|
* @return static |
70
|
|
|
* |
71
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-minlength |
72
|
|
|
*/ |
73
|
2 |
|
public function minlength(int $value): self |
74
|
|
|
{ |
75
|
2 |
|
$new = clone $this; |
76
|
2 |
|
$new->attributes['minlength'] = $value; |
77
|
2 |
|
return $new; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* It allows defining placeholder. |
82
|
|
|
* |
83
|
|
|
* @param string $value |
84
|
|
|
* |
85
|
|
|
* @return static |
86
|
|
|
* |
87
|
|
|
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.text.html#input.text.attrs.placeholder |
88
|
|
|
*/ |
89
|
3 |
|
public function placeholder(string $value): self |
90
|
|
|
{ |
91
|
3 |
|
$new = clone $this; |
92
|
3 |
|
$new->attributes['placeholder'] = $value; |
93
|
3 |
|
return $new; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* The pattern attribute, when specified, is a regular expression that the input's value must match in order for |
98
|
|
|
* the value to pass constraint validation. It must be a valid JavaScript regular expression, as used by the |
99
|
|
|
* RegExp type. |
100
|
|
|
* |
101
|
|
|
* @param string $value |
102
|
|
|
* |
103
|
|
|
* @return static |
104
|
|
|
* |
105
|
|
|
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.text.html#input.text.attrs.pattern |
106
|
|
|
*/ |
107
|
3 |
|
public function pattern(string $value): self |
108
|
|
|
{ |
109
|
3 |
|
$new = clone $this; |
110
|
3 |
|
$new->attributes['pattern'] = $value; |
111
|
3 |
|
return $new; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* A Boolean attribute which, if present, means this field cannot be edited by the user. |
116
|
|
|
* Its value can, however, still be changed by JavaScript code directly setting the HTMLInputElement.value |
117
|
|
|
* property. |
118
|
|
|
* |
119
|
|
|
* @param bool $value |
120
|
|
|
* |
121
|
|
|
* @return static |
122
|
|
|
* |
123
|
|
|
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/textarea.html#textarea.attrs.readonly |
124
|
|
|
*/ |
125
|
3 |
|
public function readonly(bool $value = true): self |
126
|
|
|
{ |
127
|
3 |
|
$new = clone $this; |
128
|
3 |
|
$new->attributes['readonly'] = $value; |
129
|
3 |
|
return $new; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* The height of the input with multiple is true. |
134
|
|
|
* |
135
|
|
|
* Default value is 4. |
136
|
|
|
* |
137
|
|
|
* @param int $value |
138
|
|
|
* |
139
|
|
|
* @return static |
140
|
|
|
* |
141
|
|
|
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.text.html#input.text.attrs.size |
142
|
|
|
*/ |
143
|
3 |
|
public function size(int $value = 4): self |
144
|
|
|
{ |
145
|
3 |
|
$new = clone $this; |
146
|
3 |
|
$new->attributes['size'] = $value; |
147
|
3 |
|
return $new; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return string the generated input tag. |
152
|
|
|
*/ |
153
|
60 |
|
protected function run(): string |
154
|
|
|
{ |
155
|
60 |
|
$new = clone $this; |
156
|
|
|
|
157
|
|
|
/** @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.text.html#input.text.attrs.value */ |
158
|
60 |
|
$value = HtmlForm::getAttributeValue($new->getFormModel(), $new->getAttribute()); |
159
|
|
|
|
160
|
|
|
|
161
|
60 |
|
if (!is_string($value) && null !== $value) { |
162
|
3 |
|
throw new InvalidArgumentException('Text widget must be a string or null value.'); |
163
|
|
|
} |
164
|
|
|
|
165
|
57 |
|
return Input::text() |
166
|
57 |
|
->attributes($new->attributes) |
167
|
57 |
|
->id($new->getId()) |
168
|
57 |
|
->name($new->getName()) |
169
|
57 |
|
->value($value === '' ? null : $value) |
170
|
57 |
|
->render(); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|