|
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
|
|
|
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
|
|
|
$rules = $formModel->getRules()[$attribute] ?? []; |
|
38
|
|
|
|
|
39
|
|
|
foreach ($rules as $rule) { |
|
40
|
|
|
if ($rule instanceof Required) { |
|
41
|
|
|
$attributes['required'] = true; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ($rule instanceof HasLength && $widget instanceof HasLengthInterface) { |
|
45
|
|
|
/** @var int|null */ |
|
46
|
|
|
$attributes['maxlength'] = $rule->getOptions()['max'] !== 0 ? $rule->getOptions()['max'] : null; |
|
47
|
|
|
/** @var int|null */ |
|
48
|
|
|
$attributes['minlength'] = $rule->getOptions()['min'] !== 0 ? $rule->getOptions()['min'] : null; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if ($rule instanceof MatchRegularExpression && $widget instanceof MatchRegularInterface) { |
|
52
|
|
|
/** @var string */ |
|
53
|
|
|
$pattern = $rule->getOptions()['pattern']; |
|
54
|
|
|
$attributes['pattern'] = Html::normalizeRegexpPattern($pattern); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if ($rule instanceof Number && $widget instanceof NumberInterface) { |
|
58
|
|
|
/** @var int|null */ |
|
59
|
|
|
$attributes['max'] = $rule->getOptions()['max'] !== 0 ? $rule->getOptions()['max'] : null; |
|
60
|
|
|
/** @var int|null */ |
|
61
|
|
|
$attributes['min'] = $rule->getOptions()['min'] !== 0 ? $rule->getOptions()['min'] : null; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if ($rule instanceof UrlValidator && $widget instanceof Url) { |
|
65
|
|
|
/** @var array<array-key, string> */ |
|
66
|
|
|
$validSchemes = $rule->getOptions()['validSchemes']; |
|
67
|
|
|
|
|
68
|
|
|
$schemes = []; |
|
69
|
|
|
|
|
70
|
|
|
foreach ($validSchemes as $scheme) { |
|
71
|
|
|
$schemes[] = $this->getSchemePattern($scheme); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** @var array<array-key, float|int|string>|string */ |
|
75
|
|
|
$pattern = $rule->getOptions()['pattern']; |
|
76
|
|
|
$normalizePattern = str_replace('{schemes}', '(' . implode('|', $schemes) . ')', $pattern); |
|
77
|
|
|
$attributes['pattern'] = Html::normalizeRegexpPattern($normalizePattern); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $attributes; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function getSchemePattern(string $scheme): string |
|
85
|
|
|
{ |
|
86
|
|
|
$result = ''; |
|
87
|
|
|
|
|
88
|
|
|
for ($i = 0, $length = mb_strlen($scheme); $i < $length; $i++) { |
|
89
|
|
|
$result .= '[' . mb_strtolower($scheme[$i]) . mb_strtoupper($scheme[$i]) . ']'; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $result; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|