1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\YiisoftFormModel; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Form\Field\Base\BaseField; |
8
|
|
|
use Yiisoft\Form\Field\Base\DateTimeInputField; |
9
|
|
|
use Yiisoft\Form\Field\Email; |
10
|
|
|
use Yiisoft\Form\Field\File; |
11
|
|
|
use Yiisoft\Form\Field\Number; |
12
|
|
|
use Yiisoft\Form\Field\Password; |
13
|
|
|
use Yiisoft\Form\Field\Range; |
14
|
|
|
use Yiisoft\Form\Field\Select; |
15
|
|
|
use Yiisoft\Form\Field\Telephone; |
16
|
|
|
use Yiisoft\Form\Field\Text; |
17
|
|
|
use Yiisoft\Form\Field\Textarea; |
18
|
|
|
use Yiisoft\Form\Field\Url; |
19
|
|
|
use Yiisoft\Form\ValidationRulesEnricherInterface; |
20
|
|
|
use Yiisoft\Html\Html; |
21
|
|
|
use Yiisoft\Validator\Rule\Length; |
22
|
|
|
use Yiisoft\Validator\Rule\Number as NumberRule; |
23
|
|
|
use Yiisoft\Validator\Rule\Regex; |
24
|
|
|
use Yiisoft\Validator\Rule\Required; |
25
|
|
|
use Yiisoft\Validator\Rule\Url as UrlRule; |
26
|
|
|
use Yiisoft\Validator\WhenInterface; |
27
|
|
|
|
28
|
|
|
use function is_iterable; |
29
|
|
|
|
30
|
|
|
final class ValidationRulesEnricher implements ValidationRulesEnricherInterface |
31
|
|
|
{ |
32
|
45 |
|
public function process(BaseField $field, mixed $rules): ?array |
33
|
|
|
{ |
34
|
45 |
|
if (!is_iterable($rules)) { |
35
|
|
|
return null; |
36
|
|
|
} |
37
|
|
|
|
38
|
45 |
|
if ($field instanceof DateTimeInputField) { |
39
|
2 |
|
$enrichment = []; |
40
|
2 |
|
foreach ($rules as $rule) { |
41
|
2 |
|
if ($rule instanceof WhenInterface && $rule->getWhen() !== null) { |
42
|
1 |
|
continue; |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
if ($rule instanceof Required) { |
46
|
1 |
|
$enrichment['inputAttributes']['required'] = true; |
47
|
|
|
} |
48
|
|
|
} |
49
|
2 |
|
return $enrichment; |
50
|
|
|
} |
51
|
|
|
|
52
|
43 |
|
if ($field instanceof Email) { |
53
|
5 |
|
$enrichment = []; |
54
|
5 |
|
foreach ($rules as $rule) { |
55
|
5 |
|
if ($rule instanceof WhenInterface && $rule->getWhen() !== null) { |
56
|
1 |
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
4 |
|
if ($rule instanceof Required) { |
60
|
1 |
|
$enrichment['inputAttributes']['required'] = true; |
61
|
|
|
} |
62
|
|
|
|
63
|
4 |
|
if ($rule instanceof Length) { |
64
|
1 |
|
if (null !== $min = $rule->getMin()) { |
65
|
1 |
|
$enrichment['inputAttributes']['minlength'] = $min; |
66
|
|
|
} |
67
|
1 |
|
if (null !== $max = $rule->getMax()) { |
68
|
1 |
|
$enrichment['inputAttributes']['maxlength'] = $max; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
4 |
|
if ($rule instanceof Regex) { |
73
|
2 |
|
if (!$rule->isNot()) { |
74
|
1 |
|
$enrichment['inputAttributes']['pattern'] = Html::normalizeRegexpPattern( |
75
|
1 |
|
$rule->getPattern() |
76
|
1 |
|
); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
5 |
|
return $enrichment; |
81
|
|
|
} |
82
|
|
|
|
83
|
38 |
|
if ($field instanceof File) { |
84
|
2 |
|
$enrichment = []; |
85
|
2 |
|
foreach ($rules as $rule) { |
86
|
2 |
|
if ($rule instanceof WhenInterface && $rule->getWhen() !== null) { |
87
|
1 |
|
continue; |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
if ($rule instanceof Required) { |
91
|
1 |
|
$enrichment['inputAttributes']['required'] = true; |
92
|
|
|
} |
93
|
|
|
} |
94
|
2 |
|
return $enrichment; |
95
|
|
|
} |
96
|
|
|
|
97
|
36 |
|
if ($field instanceof Number) { |
98
|
3 |
|
$enrichment = []; |
99
|
3 |
|
foreach ($rules as $rule) { |
100
|
3 |
|
if ($rule instanceof WhenInterface && $rule->getWhen() !== null) { |
101
|
1 |
|
continue; |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
if ($rule instanceof Required) { |
105
|
1 |
|
$enrichment['inputAttributes']['required'] = true; |
106
|
|
|
} |
107
|
|
|
|
108
|
2 |
|
if ($rule instanceof NumberRule) { |
109
|
1 |
|
if (null !== $min = $rule->getMin()) { |
110
|
1 |
|
$enrichment['inputAttributes']['min'] = $min; |
111
|
|
|
} |
112
|
1 |
|
if (null !== $max = $rule->getMax()) { |
113
|
1 |
|
$enrichment['inputAttributes']['max'] = $max; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
3 |
|
return $enrichment; |
118
|
|
|
} |
119
|
|
|
|
120
|
33 |
|
if ($field instanceof Password) { |
121
|
5 |
|
$enrichment = []; |
122
|
5 |
|
foreach ($rules as $rule) { |
123
|
5 |
|
if ($rule instanceof WhenInterface && $rule->getWhen() !== null) { |
124
|
1 |
|
continue; |
125
|
|
|
} |
126
|
|
|
|
127
|
4 |
|
if ($rule instanceof Required) { |
128
|
1 |
|
$enrichment['inputAttributes']['required'] = true; |
129
|
|
|
} |
130
|
|
|
|
131
|
4 |
|
if ($rule instanceof Length) { |
132
|
1 |
|
if (null !== $min = $rule->getMin()) { |
133
|
1 |
|
$enrichment['inputAttributes']['minlength'] = $min; |
134
|
|
|
} |
135
|
1 |
|
if (null !== $max = $rule->getMax()) { |
136
|
1 |
|
$enrichment['inputAttributes']['maxlength'] = $max; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
4 |
|
if ($rule instanceof Regex) { |
141
|
2 |
|
if (!$rule->isNot()) { |
142
|
1 |
|
$enrichment['inputAttributes']['pattern'] = Html::normalizeRegexpPattern( |
143
|
1 |
|
$rule->getPattern() |
144
|
1 |
|
); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
5 |
|
return $enrichment; |
149
|
|
|
} |
150
|
|
|
|
151
|
28 |
|
if ($field instanceof Range) { |
152
|
3 |
|
$enrichment = []; |
153
|
3 |
|
foreach ($rules as $rule) { |
154
|
3 |
|
if ($rule instanceof WhenInterface && $rule->getWhen() !== null) { |
155
|
1 |
|
continue; |
156
|
|
|
} |
157
|
|
|
|
158
|
2 |
|
if ($rule instanceof Required) { |
159
|
1 |
|
$enrichment['inputAttributes']['required'] = true; |
160
|
|
|
} |
161
|
|
|
|
162
|
2 |
|
if ($rule instanceof NumberRule) { |
163
|
1 |
|
if (null !== $min = $rule->getMin()) { |
164
|
1 |
|
$enrichment['inputAttributes']['min'] = $min; |
165
|
|
|
} |
166
|
1 |
|
if (null !== $max = $rule->getMax()) { |
167
|
1 |
|
$enrichment['inputAttributes']['max'] = $max; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
} |
171
|
3 |
|
return $enrichment; |
172
|
|
|
} |
173
|
|
|
|
174
|
25 |
|
if ($field instanceof Select) { |
175
|
2 |
|
$enrichment = []; |
176
|
2 |
|
foreach ($rules as $rule) { |
177
|
2 |
|
if ($rule instanceof WhenInterface && $rule->getWhen() !== null) { |
178
|
1 |
|
continue; |
179
|
|
|
} |
180
|
|
|
|
181
|
1 |
|
if ($rule instanceof Required) { |
182
|
1 |
|
$enrichment['inputAttributes']['required'] = true; |
183
|
|
|
} |
184
|
|
|
} |
185
|
2 |
|
return $enrichment; |
186
|
|
|
} |
187
|
|
|
|
188
|
23 |
|
if ($field instanceof Telephone) { |
189
|
5 |
|
$enrichment = []; |
190
|
5 |
|
foreach ($rules as $rule) { |
191
|
5 |
|
if ($rule instanceof WhenInterface && $rule->getWhen() !== null) { |
192
|
1 |
|
continue; |
193
|
|
|
} |
194
|
|
|
|
195
|
4 |
|
if ($rule instanceof Required) { |
196
|
1 |
|
$enrichment['inputAttributes']['required'] = true; |
197
|
|
|
} |
198
|
|
|
|
199
|
4 |
|
if ($rule instanceof Length) { |
200
|
1 |
|
if (null !== $min = $rule->getMin()) { |
201
|
1 |
|
$enrichment['inputAttributes']['minlength'] = $min; |
202
|
|
|
} |
203
|
1 |
|
if (null !== $max = $rule->getMax()) { |
204
|
1 |
|
$enrichment['inputAttributes']['maxlength'] = $max; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
4 |
|
if ($rule instanceof Regex) { |
209
|
2 |
|
if (!$rule->isNot()) { |
210
|
1 |
|
$enrichment['inputAttributes']['pattern'] = Html::normalizeRegexpPattern( |
211
|
1 |
|
$rule->getPattern() |
212
|
1 |
|
); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
} |
216
|
5 |
|
return $enrichment; |
217
|
|
|
} |
218
|
|
|
|
219
|
18 |
|
if ($field instanceof Text) { |
220
|
6 |
|
$enrichment = []; |
221
|
6 |
|
foreach ($rules as $rule) { |
222
|
6 |
|
if ($rule instanceof WhenInterface && $rule->getWhen() !== null) { |
223
|
1 |
|
continue; |
224
|
|
|
} |
225
|
|
|
|
226
|
5 |
|
if ($rule instanceof Required) { |
227
|
2 |
|
$enrichment['inputAttributes']['required'] = true; |
228
|
|
|
} |
229
|
|
|
|
230
|
5 |
|
if ($rule instanceof Length) { |
231
|
1 |
|
if (null !== $min = $rule->getMin()) { |
232
|
1 |
|
$enrichment['inputAttributes']['minlength'] = $min; |
233
|
|
|
} |
234
|
1 |
|
if (null !== $max = $rule->getMax()) { |
235
|
1 |
|
$enrichment['inputAttributes']['maxlength'] = $max; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
5 |
|
if ($rule instanceof Regex) { |
240
|
2 |
|
if (!$rule->isNot()) { |
241
|
1 |
|
$enrichment['inputAttributes']['pattern'] = Html::normalizeRegexpPattern( |
242
|
1 |
|
$rule->getPattern() |
243
|
1 |
|
); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
} |
247
|
6 |
|
return $enrichment; |
248
|
|
|
} |
249
|
|
|
|
250
|
12 |
|
if ($field instanceof Textarea) { |
251
|
3 |
|
$enrichment = []; |
252
|
3 |
|
foreach ($rules as $rule) { |
253
|
3 |
|
if ($rule instanceof WhenInterface && $rule->getWhen() !== null) { |
254
|
1 |
|
continue; |
255
|
|
|
} |
256
|
|
|
|
257
|
2 |
|
if ($rule instanceof Required) { |
258
|
1 |
|
$enrichment['inputAttributes']['required'] = true; |
259
|
|
|
} |
260
|
|
|
|
261
|
2 |
|
if ($rule instanceof Length) { |
262
|
1 |
|
if (null !== $min = $rule->getMin()) { |
263
|
1 |
|
$enrichment['inputAttributes']['minlength'] = $min; |
264
|
|
|
} |
265
|
1 |
|
if (null !== $max = $rule->getMax()) { |
266
|
1 |
|
$enrichment['inputAttributes']['maxlength'] = $max; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
} |
270
|
3 |
|
return $enrichment; |
271
|
|
|
} |
272
|
|
|
|
273
|
9 |
|
if ($field instanceof Url) { |
274
|
9 |
|
$enrichment = []; |
275
|
9 |
|
foreach ($rules as $rule) { |
276
|
9 |
|
if ($rule instanceof WhenInterface && $rule->getWhen() !== null) { |
277
|
|
|
continue; |
278
|
|
|
} |
279
|
|
|
|
280
|
9 |
|
if ($rule instanceof Required) { |
281
|
1 |
|
$enrichment['inputAttributes']['required'] = true; |
282
|
|
|
} |
283
|
|
|
|
284
|
9 |
|
if ($rule instanceof Length) { |
285
|
1 |
|
if (null !== $min = $rule->getMin()) { |
286
|
1 |
|
$enrichment['inputAttributes']['minlength'] = $min; |
287
|
|
|
} |
288
|
1 |
|
if (null !== $max = $rule->getMax()) { |
289
|
1 |
|
$enrichment['inputAttributes']['maxlength'] = $max; |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|
293
|
9 |
|
$pattern = null; |
294
|
9 |
|
if ($rule instanceof UrlRule) { |
295
|
5 |
|
$pattern = $rule->isIdnEnabled() ? null : $rule->getPattern(); |
296
|
|
|
} |
297
|
9 |
|
if ($pattern === null && $rule instanceof Regex) { |
298
|
5 |
|
if (!$rule->isNot()) { |
299
|
4 |
|
$pattern = $rule->getPattern(); |
300
|
|
|
} |
301
|
|
|
} |
302
|
9 |
|
if ($pattern !== null) { |
303
|
5 |
|
$enrichment['inputAttributes']['pattern'] = Html::normalizeRegexpPattern($pattern); |
304
|
|
|
} |
305
|
|
|
} |
306
|
9 |
|
return $enrichment; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
return null; |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|