1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Field; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Yiisoft\Form\Field\Base\AbstractField; |
9
|
|
|
|
10
|
|
|
use Yiisoft\Form\Field\Base\PlaceholderTrait; |
11
|
|
|
use Yiisoft\Html\Html; |
12
|
|
|
|
13
|
|
|
use function is_string; |
14
|
|
|
|
15
|
|
|
final class Email extends AbstractField |
16
|
|
|
{ |
17
|
|
|
use PlaceholderTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* A boolean attribute that controls whether or not the user can edit the form control. |
21
|
|
|
* |
22
|
|
|
* @param bool $value Whether to allow the value to be edited by the user. |
23
|
|
|
* |
24
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-readonly |
25
|
|
|
*/ |
26
|
|
|
public function readonly(bool $value = true): self |
27
|
|
|
{ |
28
|
|
|
$new = clone $this; |
29
|
|
|
$new->inputTagAttributes['readonly'] = $value; |
30
|
|
|
return $new; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* A boolean attribute. When specified, the element is required. |
35
|
|
|
* |
36
|
|
|
* @param bool $value Whether the control is required for form submission. |
37
|
|
|
* |
38
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-required |
39
|
|
|
*/ |
40
|
|
|
public function required(bool $value = true): self |
41
|
|
|
{ |
42
|
|
|
$new = clone $this; |
43
|
|
|
$new->inputTagAttributes['required'] = $value; |
44
|
|
|
return $new; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Allow to specify more than one value. |
49
|
|
|
* |
50
|
|
|
* @param bool $value Whether the user is to be allowed to specify more than one value. |
51
|
|
|
* |
52
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-multiple |
53
|
|
|
*/ |
54
|
|
|
public function multiple(bool $value = true): self |
55
|
|
|
{ |
56
|
|
|
$new = clone $this; |
57
|
|
|
$new->inputTagAttributes['multiple'] = $value; |
58
|
|
|
return $new; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Maximum length of value. |
63
|
|
|
* |
64
|
|
|
* @param int $value A limit on the number of characters a user can input. |
65
|
|
|
* |
66
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-maxlength |
67
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-maxlength |
68
|
|
|
*/ |
69
|
|
|
public function maxlength(int $value): self |
70
|
|
|
{ |
71
|
|
|
$new = clone $this; |
72
|
|
|
$new->inputTagAttributes['maxlength'] = $value; |
73
|
|
|
return $new; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Minimum length of value. |
78
|
|
|
* |
79
|
|
|
* @param int $value A lower bound on the number of characters a user can input. |
80
|
|
|
* |
81
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-minlength |
82
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-minlength |
83
|
|
|
*/ |
84
|
|
|
public function minlength(int $value): self |
85
|
|
|
{ |
86
|
|
|
$new = clone $this; |
87
|
|
|
$new->inputTagAttributes['minlength'] = $value; |
88
|
|
|
return $new; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Pattern to be matched by the form control's value. |
93
|
|
|
* |
94
|
|
|
* @param string $value A regular expression against which the control's value. |
95
|
|
|
* |
96
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-pattern |
97
|
|
|
*/ |
98
|
|
|
public function pattern(string $value): self |
99
|
|
|
{ |
100
|
|
|
$new = clone $this; |
101
|
|
|
$new->inputTagAttributes['pattern'] = $value; |
102
|
|
|
return $new; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* The size of the control. |
107
|
|
|
* |
108
|
|
|
* @param int $value The number of characters that allow the user to see while editing the element's value. |
109
|
|
|
* |
110
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-size |
111
|
|
|
*/ |
112
|
|
|
public function size(int $value): self |
113
|
|
|
{ |
114
|
|
|
$new = clone $this; |
115
|
|
|
$new->inputTagAttributes['size'] = $value; |
116
|
|
|
return $new; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Identifies the element (or elements) that describes the object. |
121
|
|
|
* |
122
|
|
|
* @link https://w3c.github.io/aria/#aria-describedby |
123
|
|
|
*/ |
124
|
|
|
public function ariaDescribedBy(string $value): self |
125
|
|
|
{ |
126
|
|
|
$new = clone $this; |
127
|
|
|
$new->inputTagAttributes['aria-describedby'] = $value; |
128
|
|
|
return $new; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Defines a string value that labels the current element. |
133
|
|
|
* |
134
|
|
|
* @link https://w3c.github.io/aria/#aria-label |
135
|
|
|
*/ |
136
|
|
|
public function ariaLabel(string $value): self |
137
|
|
|
{ |
138
|
|
|
$new = clone $this; |
139
|
|
|
$new->inputTagAttributes['aria-label'] = $value; |
140
|
|
|
return $new; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Specifies the form element the tag input element belongs to. The value of this attribute must be the ID |
145
|
|
|
* attribute of a form element in the same document. |
146
|
|
|
* |
147
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form |
148
|
|
|
*/ |
149
|
|
|
public function form(string $value): self |
150
|
|
|
{ |
151
|
|
|
$new = clone $this; |
152
|
|
|
$new->inputTagAttributes['form'] = $value; |
153
|
|
|
return $new; |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
protected function generateInput(): string |
157
|
|
|
{ |
158
|
1 |
|
$value = $this->getAttributeValue(); |
159
|
|
|
|
160
|
1 |
|
if (!is_string($value) && $value !== null) { |
161
|
|
|
throw new InvalidArgumentException('Email widget must be a string or null value.'); |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
$tagAttributes = $this->getInputTagAttributes(); |
165
|
|
|
|
166
|
|
|
/** @psalm-suppress MixedArgumentTypeCoercion */ |
167
|
1 |
|
return Html::input('email', $this->getInputName(), $value) |
168
|
1 |
|
->attributes($tagAttributes) |
169
|
1 |
|
->render(); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|