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
|
|
|
|
11
|
|
|
trait FormAttributeTrait |
12
|
|
|
{ |
13
|
|
|
private ?FormModelInterface $formModel = null; |
14
|
|
|
private string $attribute = ''; |
15
|
|
|
|
16
|
178 |
|
final public function attribute(FormModelInterface $formModel, string $attribute): static |
17
|
|
|
{ |
18
|
178 |
|
$new = clone $this; |
19
|
178 |
|
$new->formModel = $formModel; |
20
|
178 |
|
$new->attribute = $attribute; |
21
|
178 |
|
return $new; |
22
|
|
|
} |
23
|
|
|
|
24
|
165 |
|
final protected function getFormModel(): FormModelInterface |
25
|
|
|
{ |
26
|
165 |
|
if ($this->formModel === null) { |
27
|
|
|
throw new InvalidArgumentException('Form model is not set.'); |
28
|
|
|
} |
29
|
|
|
|
30
|
165 |
|
return $this->formModel; |
31
|
|
|
} |
32
|
|
|
|
33
|
202 |
|
final protected function hasFormModelAndAttribute(): bool |
34
|
|
|
{ |
35
|
202 |
|
return $this->formModel !== null && $this->attribute !== ''; |
36
|
|
|
} |
37
|
|
|
|
38
|
139 |
|
final protected function getAttributeName(): string |
39
|
|
|
{ |
40
|
139 |
|
return HtmlForm::getAttributeName($this->getFormModel(), $this->attribute); |
41
|
|
|
} |
42
|
|
|
|
43
|
136 |
|
final protected function getAttributeValue(): mixed |
44
|
|
|
{ |
45
|
136 |
|
return HtmlForm::getAttributeValue($this->getFormModel(), $this->attribute); |
46
|
|
|
} |
47
|
|
|
|
48
|
120 |
|
final protected function getAttributeLabel(): string |
49
|
|
|
{ |
50
|
120 |
|
return HtmlForm::getAttributeLabel($this->getFormModel(), $this->attribute); |
51
|
|
|
} |
52
|
|
|
|
53
|
137 |
|
final protected function getAttributeHint(): string |
54
|
|
|
{ |
55
|
137 |
|
return HtmlForm::getAttributeHint($this->getFormModel(), $this->attribute); |
56
|
|
|
} |
57
|
|
|
|
58
|
72 |
|
final protected function getAttributePlaceholder(): ?string |
59
|
|
|
{ |
60
|
72 |
|
$placeholder = $this->getFormModel()->getAttributePlaceholder($this->getAttributeName()); |
61
|
72 |
|
return $placeholder === '' ? null : $placeholder; |
62
|
|
|
} |
63
|
|
|
|
64
|
117 |
|
final protected function getInputId(): string |
65
|
|
|
{ |
66
|
117 |
|
return HtmlForm::getInputId($this->getFormModel(), $this->attribute); |
67
|
|
|
} |
68
|
|
|
|
69
|
139 |
|
final protected function getFirstError(): ?string |
70
|
|
|
{ |
71
|
139 |
|
return $this->getFormModel()->getFormErrors()->getFirstError($this->getAttributeName()); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|