1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Widget\FieldPart; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Yiisoft\Form\Exception\AttributeNotSetException; |
9
|
|
|
use Yiisoft\Form\Exception\FormModelNotSetException; |
10
|
|
|
use Yiisoft\Form\FormModelInterface; |
11
|
|
|
use Yiisoft\Form\Helper\HtmlForm; |
12
|
|
|
use Yiisoft\Html\Tag\CustomTag; |
13
|
|
|
use Yiisoft\Widget\Widget; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The widget for hint form. |
17
|
|
|
* |
18
|
|
|
* @psalm-suppress MissingConstructor |
19
|
|
|
*/ |
20
|
|
|
final class Hint extends Widget |
21
|
|
|
{ |
22
|
|
|
private string $attribute = ''; |
23
|
|
|
private array $attributes = []; |
24
|
|
|
private bool $encode = false; |
25
|
|
|
private ?string $hint = ''; |
26
|
|
|
private string $tag = 'div'; |
27
|
|
|
private ?FormModelInterface $formModel = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The HTML attributes. The following special options are recognized. |
31
|
|
|
* |
32
|
|
|
* @param array $values Attribute values indexed by attribute names. |
33
|
|
|
* |
34
|
|
|
* @return static |
35
|
|
|
* |
36
|
|
|
* See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
37
|
|
|
*/ |
38
|
360 |
|
public function attributes(array $values): self |
39
|
|
|
{ |
40
|
360 |
|
$new = clone $this; |
41
|
360 |
|
$new->attributes = array_merge($new->attributes, $values); |
42
|
360 |
|
return $new; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Whether content should be HTML-encoded. |
47
|
|
|
* |
48
|
|
|
* @param bool $value |
49
|
|
|
* |
50
|
|
|
* @return static |
51
|
|
|
*/ |
52
|
360 |
|
public function encode(bool $value): self |
53
|
|
|
{ |
54
|
360 |
|
$new = clone $this; |
55
|
360 |
|
$new->encode = $value; |
56
|
360 |
|
return $new; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return static |
61
|
|
|
*/ |
62
|
366 |
|
public function for(FormModelInterface $formModel, string $attribute): self |
63
|
|
|
{ |
64
|
366 |
|
$new = clone $this; |
65
|
366 |
|
$new->formModel = $formModel; |
66
|
366 |
|
$new->attribute = $attribute; |
67
|
366 |
|
return $new; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set the ID of the widget. |
72
|
|
|
* |
73
|
|
|
* @param string|null $id |
74
|
|
|
* |
75
|
|
|
* @return static |
76
|
|
|
* |
77
|
|
|
* @link https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute |
78
|
|
|
*/ |
79
|
2 |
|
public function id(?string $id): self |
80
|
|
|
{ |
81
|
2 |
|
$new = clone $this; |
82
|
2 |
|
$new->attributes['id'] = $id; |
83
|
2 |
|
return $new; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set hint text. |
88
|
|
|
* |
89
|
|
|
* @param string|null $value |
90
|
|
|
* |
91
|
|
|
* @return static |
92
|
|
|
*/ |
93
|
361 |
|
public function hint(?string $value): self |
94
|
|
|
{ |
95
|
361 |
|
$new = clone $this; |
96
|
361 |
|
$new->hint = $value; |
97
|
361 |
|
return $new; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Set the container tag name for the hint. |
102
|
|
|
* |
103
|
|
|
* @param string $value Container tag name. Set to empty value to render error messages without container tag. |
104
|
|
|
* |
105
|
|
|
* @return static |
106
|
|
|
*/ |
107
|
361 |
|
public function tag(string $value): self |
108
|
|
|
{ |
109
|
361 |
|
$new = clone $this; |
110
|
361 |
|
$new->tag = $value; |
111
|
361 |
|
return $new; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Generates a hint tag for the given form attribute. |
116
|
|
|
* |
117
|
|
|
* @return string the generated hint tag. |
118
|
|
|
*/ |
119
|
365 |
|
protected function run(): string |
120
|
|
|
{ |
121
|
365 |
|
$hint = $this->hint; |
122
|
|
|
|
123
|
365 |
|
if ($this->tag === '') { |
124
|
1 |
|
throw new InvalidArgumentException('Tag name cannot be empty.'); |
125
|
|
|
} |
126
|
|
|
|
127
|
364 |
|
if ($hint === '') { |
128
|
360 |
|
$hint = HtmlForm::getAttributeHint($this->getFormModel(), $this->getAttribute()); |
129
|
|
|
} |
130
|
|
|
|
131
|
363 |
|
return $hint !== null && $hint !== '' |
132
|
18 |
|
? CustomTag::name($this->tag) |
133
|
18 |
|
->attributes($this->attributes) |
134
|
18 |
|
->content($hint) |
135
|
18 |
|
->encode($this->encode) |
136
|
18 |
|
->render() |
137
|
363 |
|
: ''; |
138
|
|
|
} |
139
|
|
|
|
140
|
360 |
|
private function getAttribute(): string |
141
|
|
|
{ |
142
|
360 |
|
if ($this->attribute === '') { |
143
|
1 |
|
throw new AttributeNotSetException(); |
144
|
|
|
} |
145
|
|
|
|
146
|
359 |
|
return $this->attribute; |
147
|
|
|
} |
148
|
|
|
|
149
|
361 |
|
private function getFormModel(): FormModelInterface |
150
|
|
|
{ |
151
|
361 |
|
if ($this->formModel === null) { |
152
|
1 |
|
throw new FormModelNotSetException(); |
153
|
|
|
} |
154
|
|
|
|
155
|
360 |
|
return $this->formModel; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|