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