|
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
|
714 |
|
public function for(FormModelInterface $formModel, string $attribute): static |
|
19
|
|
|
{ |
|
20
|
714 |
|
$new = clone $this; |
|
21
|
714 |
|
$new->formModel = $formModel; |
|
22
|
714 |
|
$new->attribute = $attribute; |
|
23
|
714 |
|
return $new; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
711 |
|
protected function getAttribute(): string |
|
27
|
|
|
{ |
|
28
|
711 |
|
if ($this->attribute === '') { |
|
29
|
1 |
|
throw new AttributeNotSetException(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
710 |
|
return $this->attribute; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Generate label attribute. |
|
37
|
|
|
* |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
62 |
|
protected function getAttributeLabel(): string |
|
41
|
|
|
{ |
|
42
|
62 |
|
return HtmlForm::getAttributeLabel($this->getFormModel(), $this->getAttribute()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Generate placeholder attribute. |
|
47
|
|
|
* |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
371 |
|
protected function getAttributePlaceHolder(): string |
|
51
|
|
|
{ |
|
52
|
371 |
|
return HtmlForm::getAttributePlaceHolder($this->getFormModel(), $this->getAttribute()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Return value of attribute. |
|
57
|
|
|
* |
|
58
|
|
|
* @return mixed |
|
59
|
|
|
*/ |
|
60
|
676 |
|
protected function getAttributeValue(): mixed |
|
61
|
|
|
{ |
|
62
|
676 |
|
return HtmlForm::getAttributeValue($this->getFormModel(), $this->getAttribute()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Return FormModelInterface object. |
|
67
|
|
|
* |
|
68
|
|
|
* @return FormModelInterface |
|
69
|
|
|
*/ |
|
70
|
711 |
|
protected function getFormModel(): FormModelInterface |
|
71
|
|
|
{ |
|
72
|
711 |
|
if ($this->formModel === null) { |
|
73
|
1 |
|
throw new FormModelNotSetException(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
710 |
|
return $this->formModel; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Generate input id attribute. |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
665 |
|
protected function getInputId(): string |
|
85
|
|
|
{ |
|
86
|
665 |
|
return HtmlForm::getInputId($this->getFormModel(), $this->getAttribute()); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Generate input name attribute. |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
645 |
|
protected function getInputName(): string |
|
95
|
|
|
{ |
|
96
|
645 |
|
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
|
8 |
|
protected function hasError(): bool |
|
105
|
|
|
{ |
|
106
|
8 |
|
return HtmlFormErrors::hasErrors($this->getFormModel(), $this->getAttribute()); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Return if the field was validated. |
|
111
|
|
|
* |
|
112
|
|
|
* @return bool |
|
113
|
|
|
*/ |
|
114
|
6 |
|
protected function isValidated(): bool |
|
115
|
|
|
{ |
|
116
|
6 |
|
return $this->getFormModel()->isValidated(); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|