1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Widget\Validator; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Form\FormModelInterface; |
8
|
|
|
use Yiisoft\Form\Widget\Attribute\WidgetAttributes; |
9
|
|
|
use Yiisoft\Form\Widget\Url; |
10
|
|
|
use Yiisoft\Html\Html; |
11
|
|
|
use Yiisoft\Validator\Rule; |
12
|
|
|
use Yiisoft\Validator\Rule\HasLength; |
13
|
|
|
use Yiisoft\Validator\Rule\MatchRegularExpression; |
14
|
|
|
use Yiisoft\Validator\Rule\Number; |
15
|
|
|
use Yiisoft\Validator\Rule\Required; |
16
|
|
|
use Yiisoft\Validator\Rule\Url as UrlValidator; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* FieldValidator is a base class for validators that can be applied to a field. |
20
|
|
|
* |
21
|
|
|
* @param WidgetAttributes $widget The field widget. |
22
|
|
|
* @param FormModelInterface $formModel The form model instance. |
23
|
|
|
* @param string $attribute The attribute name or expression. |
24
|
|
|
* @param array $attributes The HTML attributes for the field widget. |
25
|
|
|
* |
26
|
|
|
* @return array The attributes for validator html. |
27
|
|
|
*/ |
28
|
|
|
final class FieldValidator |
29
|
|
|
{ |
30
|
615 |
|
public function getValidatorAttributes( |
31
|
|
|
WidgetAttributes $widget, |
32
|
|
|
FormModelInterface $formModel, |
33
|
|
|
string $attribute, |
34
|
|
|
array $attributes |
35
|
|
|
): array { |
36
|
|
|
/** @psalm-var array<array-key, Rule> */ |
37
|
615 |
|
$rules = $formModel->getRules()[$attribute] ?? []; |
38
|
|
|
|
39
|
615 |
|
foreach ($rules as $rule) { |
40
|
81 |
|
if ($rule instanceof Required) { |
41
|
41 |
|
$attributes['required'] = true; |
42
|
|
|
} |
43
|
|
|
|
44
|
81 |
|
if ($rule instanceof HasLength && $widget instanceof HasLengthInterface) { |
45
|
|
|
/** @var int|null */ |
46
|
32 |
|
$attributes['maxlength'] = $rule->getOptions()['max'] !== 0 ? $rule->getOptions()['max'] : null; |
47
|
|
|
/** @var int|null */ |
48
|
32 |
|
$attributes['minlength'] = $rule->getOptions()['min'] !== 0 ? $rule->getOptions()['min'] : null; |
49
|
|
|
} |
50
|
|
|
|
51
|
81 |
|
if ($rule instanceof MatchRegularExpression && $widget instanceof MatchRegularInterface) { |
52
|
|
|
/** @var string */ |
53
|
10 |
|
$pattern = $rule->getOptions()['pattern']; |
54
|
10 |
|
$attributes['pattern'] = Html::normalizeRegexpPattern($pattern); |
55
|
|
|
} |
56
|
|
|
|
57
|
81 |
|
if ($rule instanceof Number && $widget instanceof NumberInterface) { |
58
|
|
|
/** @var int|null */ |
59
|
4 |
|
$attributes['max'] = $rule->getOptions()['max'] !== 0 ? $rule->getOptions()['max'] : null; |
60
|
|
|
/** @var int|null */ |
61
|
4 |
|
$attributes['min'] = $rule->getOptions()['min'] !== 0 ? $rule->getOptions()['min'] : null; |
62
|
|
|
} |
63
|
|
|
|
64
|
81 |
|
if ($rule instanceof UrlValidator && $widget instanceof Url) { |
65
|
|
|
/** @var array<array-key, string> */ |
66
|
2 |
|
$validSchemes = $rule->getOptions()['validSchemes']; |
67
|
|
|
|
68
|
2 |
|
$schemes = []; |
69
|
|
|
|
70
|
2 |
|
foreach ($validSchemes as $scheme) { |
71
|
2 |
|
$schemes[] = $this->getSchemePattern($scheme); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** @var array<array-key, float|int|string>|string */ |
75
|
2 |
|
$pattern = $rule->getOptions()['pattern']; |
76
|
2 |
|
$normalizePattern = str_replace('{schemes}', '(' . implode('|', $schemes) . ')', $pattern); |
77
|
2 |
|
$attributes['pattern'] = Html::normalizeRegexpPattern($normalizePattern); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
615 |
|
return $attributes; |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
private function getSchemePattern(string $scheme): string |
85
|
|
|
{ |
86
|
2 |
|
$result = ''; |
87
|
|
|
|
88
|
2 |
|
for ($i = 0, $length = mb_strlen($scheme); $i < $length; $i++) { |
89
|
2 |
|
$result .= '[' . mb_strtolower($scheme[$i]) . mb_strtoupper($scheme[$i]) . ']'; |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
return $result; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|