|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Widget; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Yiisoft\Form\Widget\Attribute\InputAttributes; |
|
9
|
|
|
use Yiisoft\Form\Widget\Attribute\PlaceholderInterface; |
|
10
|
|
|
use Yiisoft\Form\Widget\Validator\HasLengthInterface; |
|
11
|
|
|
use Yiisoft\Form\Widget\Validator\MatchRegularInterface; |
|
12
|
|
|
use Yiisoft\Html\Tag\Input; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Generates a text input tag for the given form attribute. |
|
16
|
|
|
* |
|
17
|
|
|
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.text.html#input.text |
|
18
|
|
|
*/ |
|
19
|
|
|
final class Text extends InputAttributes implements HasLengthInterface, MatchRegularInterface, PlaceholderInterface |
|
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
|
|
|
public function dirname(string $value): self |
|
32
|
|
|
{ |
|
33
|
|
|
if (empty($value)) { |
|
34
|
|
|
throw new InvalidArgumentException('The value cannot be empty.'); |
|
35
|
|
|
} |
|
36
|
5 |
|
|
|
37
|
|
|
$new = clone $this; |
|
38
|
5 |
|
$new->attributes['dirname'] = $value; |
|
39
|
2 |
|
return $new; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
3 |
|
public function maxlength(int $value): self |
|
43
|
3 |
|
{ |
|
44
|
3 |
|
$new = clone $this; |
|
45
|
|
|
$new->attributes['maxlength'] = $value; |
|
46
|
|
|
return $new; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function minlength(int $value): self |
|
50
|
|
|
{ |
|
51
|
|
|
$new = clone $this; |
|
52
|
|
|
$new->attributes['minlength'] = $value; |
|
53
|
|
|
return $new; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* It allows defining placeholder. |
|
58
|
|
|
* |
|
59
|
2 |
|
* @param string $value |
|
60
|
|
|
* |
|
61
|
2 |
|
* @return static |
|
62
|
2 |
|
* |
|
63
|
2 |
|
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.text.html#input.text.attrs.placeholder |
|
64
|
|
|
*/ |
|
65
|
|
|
public function placeholder(string $value): self |
|
66
|
|
|
{ |
|
67
|
|
|
$new = clone $this; |
|
68
|
|
|
$new->attributes['placeholder'] = $value; |
|
69
|
|
|
return $new; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function pattern(string $value): self |
|
73
|
|
|
{ |
|
74
|
|
|
$new = clone $this; |
|
75
|
|
|
$new->attributes['pattern'] = $value; |
|
76
|
|
|
return $new; |
|
77
|
|
|
} |
|
78
|
1 |
|
|
|
79
|
|
|
/** |
|
80
|
1 |
|
* A Boolean attribute which, if present, means this field cannot be edited by the user. |
|
81
|
1 |
|
* Its value can, however, still be changed by JavaScript code directly setting the HTMLInputElement.value |
|
82
|
1 |
|
* property. |
|
83
|
|
|
* |
|
84
|
|
|
* @param bool $value |
|
85
|
|
|
* |
|
86
|
|
|
* @return static |
|
87
|
|
|
* |
|
88
|
|
|
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/textarea.html#textarea.attrs.readonly |
|
89
|
|
|
*/ |
|
90
|
|
|
public function readonly(bool $value = true): self |
|
91
|
|
|
{ |
|
92
|
|
|
$new = clone $this; |
|
93
|
|
|
$new->attributes['readonly'] = $value; |
|
94
|
2 |
|
return $new; |
|
95
|
|
|
} |
|
96
|
2 |
|
|
|
97
|
2 |
|
/** |
|
98
|
2 |
|
* The height of the input with multiple is true. |
|
99
|
|
|
* |
|
100
|
|
|
* @param int $value |
|
101
|
|
|
* |
|
102
|
|
|
* @return static |
|
103
|
|
|
* |
|
104
|
|
|
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.text.html#input.text.attrs.size |
|
105
|
|
|
*/ |
|
106
|
|
|
public function size(int $value): self |
|
107
|
|
|
{ |
|
108
|
|
|
$new = clone $this; |
|
109
|
|
|
$new->attributes['size'] = $value; |
|
110
|
|
|
return $new; |
|
111
|
|
|
} |
|
112
|
2 |
|
|
|
113
|
|
|
/** |
|
114
|
2 |
|
* @return string the generated input tag. |
|
115
|
2 |
|
*/ |
|
116
|
2 |
|
protected function run(): string |
|
117
|
|
|
{ |
|
118
|
|
|
$attributes = $this->build($this->attributes); |
|
119
|
|
|
|
|
120
|
|
|
/** @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.text.html#input.text.attrs.value */ |
|
121
|
|
|
$value = $attributes['value'] ?? $this->getAttributeValue(); |
|
122
|
|
|
unset($attributes['value']); |
|
123
|
|
|
|
|
124
|
|
|
if (null !== $value && !is_string($value)) { |
|
125
|
|
|
throw new InvalidArgumentException('Text widget must be a string or null value.'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return Input::tag()->type('text')->attributes($attributes)->value($value === '' ? null : $value)->render(); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|