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
|
|
|
/** |
17
|
|
|
* @return static |
18
|
|
|
*/ |
19
|
71 |
|
final public function attribute(FormModelInterface $formModel, string $attribute): self |
20
|
|
|
{ |
21
|
71 |
|
$new = clone $this; |
22
|
71 |
|
$new->formModel = $formModel; |
23
|
71 |
|
$new->attribute = $attribute; |
24
|
71 |
|
return $new; |
25
|
|
|
} |
26
|
|
|
|
27
|
64 |
|
final protected function getFormModel(): FormModelInterface |
28
|
|
|
{ |
29
|
64 |
|
if ($this->formModel === null) { |
30
|
1 |
|
throw new InvalidArgumentException('Form model is not set.'); |
31
|
|
|
} |
32
|
|
|
|
33
|
63 |
|
return $this->formModel; |
34
|
|
|
} |
35
|
|
|
|
36
|
43 |
|
final protected function getAttributeName(): string |
37
|
|
|
{ |
38
|
43 |
|
return HtmlForm::getAttributeName($this->getFormModel(), $this->attribute); |
39
|
|
|
} |
40
|
|
|
|
41
|
29 |
|
final protected function getAttributeValue(): mixed |
42
|
|
|
{ |
43
|
29 |
|
return HtmlForm::getAttributeValue($this->getFormModel(), $this->attribute); |
44
|
|
|
} |
45
|
|
|
|
46
|
35 |
|
final protected function getAttributeLabel(): string |
47
|
|
|
{ |
48
|
35 |
|
return HtmlForm::getAttributeLabel($this->getFormModel(), $this->attribute); |
49
|
|
|
} |
50
|
|
|
|
51
|
36 |
|
final protected function getAttributeHint(): string |
52
|
|
|
{ |
53
|
36 |
|
return HtmlForm::getAttributeHint($this->getFormModel(), $this->attribute); |
54
|
|
|
} |
55
|
|
|
|
56
|
23 |
|
final protected function getAttributePlaceholder(): ?string |
57
|
|
|
{ |
58
|
23 |
|
$placeholder = $this->getFormModel()->getAttributePlaceholder($this->getAttributeName()); |
59
|
23 |
|
return $placeholder === '' ? null : $placeholder; |
60
|
|
|
} |
61
|
|
|
|
62
|
32 |
|
final protected function getInputId(): string |
63
|
|
|
{ |
64
|
32 |
|
return HtmlForm::getInputId($this->getFormModel(), $this->attribute); |
65
|
|
|
} |
66
|
|
|
|
67
|
43 |
|
final protected function getFirstError(): ?string |
68
|
|
|
{ |
69
|
43 |
|
return $this->getFormModel()->getFormErrors()->getFirstError($this->getAttributeName()); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|