|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Widget; |
|
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 array $attributes = []; |
|
21
|
|
|
private string $attribute = ''; |
|
22
|
|
|
private bool $encode = true; |
|
23
|
|
|
private FormModelInterface $formModel; |
|
24
|
|
|
private ?string $hint = ''; |
|
25
|
|
|
private string $tag = 'div'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Specify a form, its attribute and a list HTML attributes for the hint generated. |
|
29
|
|
|
* |
|
30
|
|
|
* @param FormModelInterface $formModel Form instance. |
|
31
|
|
|
* @param string $attribute Form model's property name this widget is rendered for. |
|
32
|
|
|
* @param array $attributes HTML attributes for the widget container tag. |
|
33
|
|
|
* |
|
34
|
|
|
* @return static |
|
35
|
|
|
* |
|
36
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
|
37
|
|
|
*/ |
|
38
|
157 |
|
public function config(FormModelInterface $formModel, string $attribute, array $attributes = []): self |
|
39
|
|
|
{ |
|
40
|
157 |
|
$new = clone $this; |
|
41
|
157 |
|
$new->formModel = $formModel; |
|
42
|
157 |
|
$new->attribute = $attribute; |
|
43
|
157 |
|
$new->attributes = $attributes; |
|
44
|
157 |
|
return $new; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Whether content should be HTML-encoded. |
|
49
|
|
|
* |
|
50
|
|
|
* @param bool $value |
|
51
|
|
|
* |
|
52
|
|
|
* @return static |
|
53
|
|
|
*/ |
|
54
|
153 |
|
public function encode(bool $value): self |
|
55
|
|
|
{ |
|
56
|
153 |
|
$new = clone $this; |
|
57
|
153 |
|
$new->encode = $value; |
|
58
|
153 |
|
return $new; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Set hint text. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string|null $value |
|
65
|
|
|
* |
|
66
|
|
|
* @return static |
|
67
|
|
|
*/ |
|
68
|
154 |
|
public function hint(?string $value): self |
|
69
|
|
|
{ |
|
70
|
154 |
|
$new = clone $this; |
|
71
|
154 |
|
$new->hint = $value; |
|
72
|
154 |
|
return $new; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Set the container tag name for the hint. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $value Container tag name. Set to empty value to render error messages without container tag. |
|
79
|
|
|
* |
|
80
|
|
|
* @return static |
|
81
|
|
|
*/ |
|
82
|
3 |
|
public function tag(string $value): self |
|
83
|
|
|
{ |
|
84
|
3 |
|
$new = clone $this; |
|
85
|
3 |
|
$new->tag = $value; |
|
86
|
3 |
|
return $new; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Generates a hint tag for the given form attribute. |
|
91
|
|
|
* |
|
92
|
|
|
* @return string the generated hint tag. |
|
93
|
|
|
*/ |
|
94
|
157 |
|
protected function run(): string |
|
95
|
|
|
{ |
|
96
|
157 |
|
$new = clone $this; |
|
97
|
|
|
|
|
98
|
157 |
|
if ($new->tag === '') { |
|
99
|
1 |
|
throw new InvalidArgumentException('Tag name cannot be empty.'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
156 |
|
if ($new->hint !== null && $new->hint === '') { |
|
103
|
151 |
|
$new->hint = HtmlForm::getAttributeHint($new->formModel, $new->attribute); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
156 |
|
return (!empty($new->hint)) |
|
107
|
77 |
|
? CustomTag::name($new->tag) |
|
108
|
77 |
|
->attributes($new->attributes) |
|
109
|
77 |
|
->content($new->hint) |
|
110
|
77 |
|
->encode($new->encode) |
|
111
|
77 |
|
->render() |
|
112
|
156 |
|
: ''; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|