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