1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Field\Base; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use ReflectionClass; |
9
|
|
|
use Yiisoft\Form\Field\Base\EnrichFromValidationRules\EnrichFromValidationRulesInterface; |
10
|
|
|
use Yiisoft\Form\Field\Base\EnrichFromValidationRules\EnrichFromValidationRulesTrait; |
11
|
|
|
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface; |
12
|
|
|
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait; |
13
|
|
|
use Yiisoft\Form\ThemeContainer; |
14
|
|
|
use Yiisoft\Html\Html; |
15
|
|
|
|
16
|
|
|
use function is_string; |
17
|
|
|
|
18
|
|
|
abstract class DateTimeInputField extends InputField implements EnrichFromValidationRulesInterface, ValidationClassInterface |
19
|
|
|
{ |
20
|
|
|
use EnrichFromValidationRulesTrait; |
21
|
|
|
use ValidationClassTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-max |
25
|
|
|
*/ |
26
|
4 |
|
final public function max(?string $value): static |
27
|
|
|
{ |
28
|
4 |
|
$new = clone $this; |
29
|
4 |
|
$new->inputAttributes['max'] = $value; |
30
|
4 |
|
return $new; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-min |
35
|
|
|
*/ |
36
|
4 |
|
final public function min(?string $value): static |
37
|
|
|
{ |
38
|
4 |
|
$new = clone $this; |
39
|
4 |
|
$new->inputAttributes['min'] = $value; |
40
|
4 |
|
return $new; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Identifies the element (or elements) that describes the object. |
45
|
|
|
* |
46
|
|
|
* @link https://w3c.github.io/aria/#aria-describedby |
47
|
|
|
*/ |
48
|
2 |
|
final public function ariaDescribedBy(?string $value): static |
49
|
|
|
{ |
50
|
2 |
|
$new = clone $this; |
51
|
2 |
|
$new->inputAttributes['aria-describedby'] = $value; |
52
|
2 |
|
return $new; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Defines a string value that labels the current element. |
57
|
|
|
* |
58
|
|
|
* @link https://w3c.github.io/aria/#aria-label |
59
|
|
|
*/ |
60
|
2 |
|
final public function ariaLabel(?string $value): static |
61
|
|
|
{ |
62
|
2 |
|
$new = clone $this; |
63
|
2 |
|
$new->inputAttributes['aria-label'] = $value; |
64
|
2 |
|
return $new; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Focus on the control (put cursor into it) when the page loads. Only one form element could be in focus |
69
|
|
|
* at the same time. |
70
|
|
|
* |
71
|
|
|
* @link https://html.spec.whatwg.org/multipage/interaction.html#attr-fe-autofocus |
72
|
|
|
*/ |
73
|
2 |
|
final public function autofocus(bool $value = true): static |
74
|
|
|
{ |
75
|
2 |
|
$new = clone $this; |
76
|
2 |
|
$new->inputAttributes['autofocus'] = $value; |
77
|
2 |
|
return $new; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* The `tabindex` attribute indicates that its element can be focused, and where it participates in sequential |
82
|
|
|
* keyboard navigation (usually with the Tab key, hence the name). |
83
|
|
|
* |
84
|
|
|
* It accepts an integer as a value, with different results depending on the integer's value: |
85
|
|
|
* |
86
|
|
|
* - A negative value (usually `tabindex="-1"`) means that the element is not reachable via sequential keyboard |
87
|
|
|
* navigation, but could be focused with Javascript or visually. It's mostly useful to create accessible widgets |
88
|
|
|
* with JavaScript. |
89
|
|
|
* - `tabindex="0"` means that the element should be focusable in sequential keyboard navigation, but its order is |
90
|
|
|
* defined by the document's source order. |
91
|
|
|
* - A positive value means the element should be focusable in sequential keyboard navigation, with its order |
92
|
|
|
* defined by the value of the number. That is, `tabindex="4"` is focused before `tabindex="5"`, but after |
93
|
|
|
* `tabindex="3"`. |
94
|
|
|
* |
95
|
|
|
* @link https://html.spec.whatwg.org/multipage/interaction.html#attr-tabindex |
96
|
|
|
*/ |
97
|
2 |
|
final public function tabIndex(?int $value): static |
98
|
|
|
{ |
99
|
2 |
|
$new = clone $this; |
100
|
2 |
|
$new->inputAttributes['tabindex'] = $value; |
101
|
2 |
|
return $new; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* A boolean attribute that controls whether or not the user can edit the form control. |
106
|
|
|
* |
107
|
|
|
* @param bool $value Whether to allow the value to be edited by the user. |
108
|
|
|
* |
109
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-readonly |
110
|
|
|
*/ |
111
|
2 |
|
final public function readonly(bool $value = true): static |
112
|
|
|
{ |
113
|
2 |
|
$new = clone $this; |
114
|
2 |
|
$new->inputAttributes['readonly'] = $value; |
115
|
2 |
|
return $new; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* A boolean attribute. When specified, the element is required. |
120
|
|
|
* |
121
|
|
|
* @param bool $value Whether the control is required for form submission. |
122
|
|
|
* |
123
|
|
|
* @link https://html.spec.whatwg.org/multipage/input.html#attr-input-required |
124
|
|
|
*/ |
125
|
3 |
|
final public function required(bool $value = true): static |
126
|
|
|
{ |
127
|
3 |
|
$new = clone $this; |
128
|
3 |
|
$new->inputAttributes['required'] = $value; |
129
|
3 |
|
return $new; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-disabled |
134
|
|
|
*/ |
135
|
2 |
|
final public function disabled(bool $disabled = true): static |
136
|
|
|
{ |
137
|
2 |
|
$new = clone $this; |
138
|
2 |
|
$new->inputAttributes['disabled'] = $disabled; |
139
|
2 |
|
return $new; |
140
|
|
|
} |
141
|
|
|
|
142
|
17 |
|
protected function beforeRender(): void |
143
|
|
|
{ |
144
|
17 |
|
parent::beforeRender(); |
145
|
17 |
|
if ($this->enrichFromValidationRules) { |
146
|
|
|
$this->enrichment = ThemeContainer::getEnrichment($this, $this->getInputData()); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
17 |
|
final protected function generateInput(): string |
151
|
|
|
{ |
152
|
17 |
|
$value = $this->getValue(); |
153
|
|
|
|
154
|
17 |
|
if (!is_string($value) && $value !== null) { |
155
|
1 |
|
throw new InvalidArgumentException( |
156
|
1 |
|
(new ReflectionClass($this))->getShortName() . |
157
|
1 |
|
' field requires a string or null value.' |
158
|
1 |
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** @psalm-suppress MixedArgument We guess that enrichment contain correct values. */ |
162
|
16 |
|
$inputAttributes = array_merge( |
163
|
16 |
|
$this->enrichment['inputAttributes'] ?? [], |
164
|
16 |
|
$this->getInputAttributes() |
165
|
16 |
|
); |
166
|
|
|
|
167
|
16 |
|
return Html::input($this->getInputType(), $this->getName(), $value, $inputAttributes)->render(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
abstract protected function getInputType(): string; |
171
|
|
|
|
172
|
16 |
|
protected function prepareContainerAttributes(array &$attributes): void |
173
|
|
|
{ |
174
|
16 |
|
$this->addValidationClassToAttributes($attributes, $this->getInputData()); |
175
|
|
|
} |
176
|
|
|
|
177
|
16 |
|
protected function prepareInputAttributes(array &$attributes): void |
178
|
|
|
{ |
179
|
16 |
|
$this->addInputValidationClassToAttributes($attributes, $this->getInputData()); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|