1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Field\Base\ValidationClass; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Form\Field\Base\InputData\InputDataInterface; |
8
|
|
|
use Yiisoft\Html\Html; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @psalm-require-extends \Yiisoft\Form\Field\Base\PartsField |
12
|
|
|
*/ |
13
|
|
|
trait ValidationClassTrait |
14
|
|
|
{ |
15
|
|
|
private ?string $invalidClass = null; |
16
|
|
|
private ?string $validClass = null; |
17
|
|
|
private ?string $inputInvalidClass = null; |
18
|
|
|
private ?string $inputValidClass = null; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Set invalid CSS class. |
22
|
|
|
*/ |
23
|
35 |
|
public function invalidClass(?string $class): self |
24
|
|
|
{ |
25
|
35 |
|
$new = clone $this; |
26
|
35 |
|
$new->invalidClass = $class; |
27
|
35 |
|
return $new; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Set valid CSS class. |
32
|
|
|
*/ |
33
|
21 |
|
public function validClass(?string $class): self |
34
|
|
|
{ |
35
|
21 |
|
$new = clone $this; |
36
|
21 |
|
$new->validClass = $class; |
37
|
21 |
|
return $new; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set invalid CSS class for input tag. |
42
|
|
|
*/ |
43
|
32 |
|
public function inputInvalidClass(?string $class): self |
44
|
|
|
{ |
45
|
32 |
|
$new = clone $this; |
46
|
32 |
|
$new->inputInvalidClass = $class; |
47
|
32 |
|
return $new; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Set valid CSS class for input tag. |
52
|
|
|
*/ |
53
|
32 |
|
public function inputValidClass(?string $class): self |
54
|
|
|
{ |
55
|
32 |
|
$new = clone $this; |
56
|
32 |
|
$new->inputValidClass = $class; |
57
|
32 |
|
return $new; |
58
|
|
|
} |
59
|
|
|
|
60
|
282 |
|
protected function addValidationClassToAttributes( |
61
|
|
|
array &$attributes, |
62
|
|
|
InputDataInterface $inputData, |
63
|
|
|
?bool $hasCustomError = null, |
64
|
|
|
): void { |
65
|
282 |
|
$this->addClassesToAttributes( |
66
|
282 |
|
$attributes, |
67
|
282 |
|
$inputData, |
68
|
282 |
|
$hasCustomError, |
69
|
282 |
|
$this->invalidClass, |
70
|
282 |
|
$this->validClass, |
71
|
282 |
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
417 |
|
protected function addInputValidationClassToAttributes( |
75
|
|
|
array &$attributes, |
76
|
|
|
InputDataInterface $inputData, |
77
|
|
|
?bool $hasCustomError = null, |
78
|
|
|
): void { |
79
|
417 |
|
$this->addClassesToAttributes( |
80
|
417 |
|
$attributes, |
81
|
417 |
|
$inputData, |
82
|
417 |
|
$hasCustomError, |
83
|
417 |
|
$this->inputInvalidClass, |
84
|
417 |
|
$this->inputValidClass, |
85
|
417 |
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
417 |
|
private function addClassesToAttributes( |
89
|
|
|
array &$attributes, |
90
|
|
|
InputDataInterface $inputData, |
91
|
|
|
?bool $hasCustomError, |
92
|
|
|
?string $invalidClass, |
93
|
|
|
?string $validClass, |
94
|
|
|
): void { |
95
|
417 |
|
if (!$inputData->isValidated() && $hasCustomError === null) { |
96
|
352 |
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
65 |
|
$hasErrors = $hasCustomError || !empty($inputData->getValidationErrors()); |
100
|
|
|
|
101
|
65 |
|
if ($hasErrors && $invalidClass !== null) { |
102
|
19 |
|
Html::addCssClass($attributes, $invalidClass); |
103
|
|
|
} |
104
|
|
|
|
105
|
65 |
|
if (!$hasErrors && $validClass !== null) { |
106
|
29 |
|
Html::addCssClass($attributes, $validClass); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|