|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Field\Base; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Yiisoft\Form\FormModelInterface; |
|
9
|
|
|
use Yiisoft\Form\Helper\HtmlForm; |
|
10
|
|
|
use Yiisoft\Validator\Helper\RulesNormalizer; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @psalm-import-type NormalizedRulesMap from RulesNormalizer |
|
14
|
|
|
* @psalm-import-type NormalizedRulesList from RulesNormalizer |
|
15
|
|
|
*/ |
|
16
|
|
|
trait FormAttributeTrait |
|
17
|
|
|
{ |
|
18
|
|
|
private ?FormModelInterface $formModel = null; |
|
19
|
|
|
private string $formAttribute = ''; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @psalm-var NormalizedRulesMap|null |
|
23
|
|
|
*/ |
|
24
|
|
|
private ?array $formModelValidationRules = null; |
|
25
|
|
|
|
|
26
|
492 |
|
final public function formAttribute(FormModelInterface $model, string $attribute): static |
|
27
|
|
|
{ |
|
28
|
492 |
|
$new = clone $this; |
|
29
|
492 |
|
$new->formModel = $model; |
|
30
|
492 |
|
$new->formAttribute = $attribute; |
|
31
|
492 |
|
$new->formModelValidationRules = null; |
|
32
|
492 |
|
return $new; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
480 |
|
final protected function getFormModel(): FormModelInterface |
|
36
|
|
|
{ |
|
37
|
480 |
|
if ($this->formModel === null) { |
|
38
|
1 |
|
throw new InvalidArgumentException('Form model is not set.'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
479 |
|
return $this->formModel; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @psalm-return NormalizedRulesList |
|
46
|
|
|
*/ |
|
47
|
44 |
|
final protected function getFormAttributeValidationRules(): iterable |
|
48
|
|
|
{ |
|
49
|
44 |
|
$this->formModelValidationRules ??= RulesNormalizer::normalize(null, $this->getFormModel()); |
|
50
|
44 |
|
return $this->formModelValidationRules[$this->formAttribute] ?? []; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
585 |
|
final protected function hasFormModelAndAttribute(): bool |
|
54
|
|
|
{ |
|
55
|
585 |
|
return $this->formModel !== null && $this->formAttribute !== ''; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
402 |
|
final protected function getFormAttributeName(): string |
|
59
|
|
|
{ |
|
60
|
402 |
|
return HtmlForm::getAttributeName($this->getFormModel(), $this->formAttribute); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
372 |
|
final protected function getFormAttributeValue(): mixed |
|
64
|
|
|
{ |
|
65
|
372 |
|
return HtmlForm::getAttributeValue($this->getFormModel(), $this->formAttribute); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
200 |
|
final protected function getFormAttributeLabel(): string |
|
69
|
|
|
{ |
|
70
|
200 |
|
return HtmlForm::getAttributeLabel($this->getFormModel(), $this->formAttribute); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
400 |
|
final protected function getFormAttributeHint(): string |
|
74
|
|
|
{ |
|
75
|
400 |
|
return HtmlForm::getAttributeHint($this->getFormModel(), $this->formAttribute); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
180 |
|
final protected function getFormAttributePlaceholder(): ?string |
|
79
|
|
|
{ |
|
80
|
180 |
|
$placeholder = $this |
|
81
|
180 |
|
->getFormModel() |
|
82
|
180 |
|
->getAttributePlaceholder($this->getFormAttributeName()); |
|
83
|
180 |
|
return $placeholder === '' ? null : $placeholder; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
358 |
|
final protected function getInputId(): string |
|
87
|
|
|
{ |
|
88
|
358 |
|
return HtmlForm::getInputId($this->getFormModel(), $this->formAttribute); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
402 |
|
final protected function getFirstError(): ?string |
|
92
|
|
|
{ |
|
93
|
402 |
|
return $this |
|
94
|
402 |
|
->getFormModel() |
|
95
|
402 |
|
->getFormErrors() |
|
96
|
402 |
|
->getFirstError($this->getFormAttributeName()); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|