1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Widget\Attribute; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Form\Exception\AttributeNotSetException; |
8
|
|
|
use Yiisoft\Form\Exception\FormModelNotSetException; |
9
|
|
|
use Yiisoft\Form\FormModelInterface; |
10
|
|
|
use Yiisoft\Form\Helper\HtmlForm; |
11
|
|
|
use Yiisoft\Form\Helper\HtmlFormErrors; |
12
|
|
|
|
13
|
|
|
abstract class WidgetAttributes extends GlobalAttributes |
14
|
|
|
{ |
15
|
|
|
private string $attribute = ''; |
16
|
|
|
private ?FormModelInterface $formModel = null; |
17
|
|
|
|
18
|
27 |
|
public function for(FormModelInterface $formModel, string $attribute): static |
19
|
|
|
{ |
20
|
27 |
|
$new = clone $this; |
21
|
27 |
|
$new->formModel = $formModel; |
22
|
27 |
|
$new->attribute = $attribute; |
23
|
27 |
|
return $new; |
24
|
|
|
} |
25
|
|
|
|
26
|
27 |
|
protected function getAttribute(): string |
27
|
|
|
{ |
28
|
27 |
|
if ($this->attribute === '') { |
29
|
1 |
|
throw new AttributeNotSetException(); |
30
|
|
|
} |
31
|
|
|
|
32
|
26 |
|
return $this->attribute; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Generate label attribute. |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
protected function getAttributeLabel(): string |
41
|
|
|
{ |
42
|
|
|
return HtmlForm::getAttributeLabel($this->getFormModel(), $this->getAttribute()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Generate placeholder attribute. |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
protected function getAttributePlaceHolder(): string |
51
|
|
|
{ |
52
|
|
|
return HtmlForm::getAttributePlaceHolder($this->getFormModel(), $this->getAttribute()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Return value of attribute. |
57
|
|
|
* |
58
|
|
|
* @return mixed |
59
|
|
|
*/ |
60
|
|
|
protected function getAttributeValue(): mixed |
61
|
|
|
{ |
62
|
|
|
return HtmlForm::getAttributeValue($this->getFormModel(), $this->getAttribute()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Return FormModelInterface object. |
67
|
|
|
* |
68
|
|
|
* @return FormModelInterface |
69
|
|
|
*/ |
70
|
27 |
|
protected function getFormModel(): FormModelInterface |
71
|
|
|
{ |
72
|
27 |
|
if ($this->formModel === null) { |
73
|
1 |
|
throw new FormModelNotSetException(); |
74
|
|
|
} |
75
|
|
|
|
76
|
26 |
|
return $this->formModel; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Generate input id attribute. |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
24 |
|
protected function getInputId(): string |
85
|
|
|
{ |
86
|
24 |
|
return HtmlForm::getInputId($this->getFormModel(), $this->getAttribute()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Generate input name attribute. |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
24 |
|
protected function getInputName(): string |
95
|
|
|
{ |
96
|
24 |
|
return HtmlForm::getInputName($this->getFormModel(), $this->getAttribute()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return if there is a validation error in the attribute. |
101
|
|
|
* |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
|
|
protected function hasError(): bool |
105
|
|
|
{ |
106
|
|
|
return HtmlFormErrors::hasErrors($this->getFormModel(), $this->getAttribute()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Return if the field was validated. |
111
|
|
|
* |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
protected function isValidated(): bool |
115
|
|
|
{ |
116
|
|
|
return $this->getFormModel()->isValidated(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|