1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Widget; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
8
|
|
|
use Yiisoft\Form\FormModelInterface; |
9
|
|
|
use Yiisoft\Html\Html; |
10
|
|
|
use Yiisoft\Widget\Widget; |
11
|
|
|
|
12
|
|
|
final class Hint extends Widget |
13
|
|
|
{ |
14
|
|
|
private FormModelInterface $data; |
15
|
|
|
private string $attribute; |
16
|
|
|
private array $options = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Generates a hint tag for the given form attribute. |
20
|
|
|
* |
21
|
|
|
* @return string the generated hint tag. |
22
|
|
|
*/ |
23
|
|
|
public function run(): string |
24
|
|
|
{ |
25
|
|
|
$new = clone $this; |
26
|
|
|
|
27
|
|
|
$hint = ArrayHelper::remove($new->options, 'hint', $new->data->getAttributeHint($new->attribute)); |
28
|
|
|
|
29
|
|
|
if (empty($hint)) { |
30
|
|
|
return ''; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$tag = ArrayHelper::remove($new->options, 'tag', 'div'); |
34
|
|
|
if (empty($tag)) { |
35
|
|
|
return $hint; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$encode = ArrayHelper::remove($new->options, 'encode', true); |
39
|
|
|
|
40
|
|
|
return Html::tag($tag, $hint, $new->options) |
41
|
|
|
->encode($encode) |
42
|
|
|
->render(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Set form model, name and options for the widget. |
47
|
|
|
* |
48
|
|
|
* @param FormModelInterface $data Form model. |
49
|
|
|
* @param string $attribute Form model property this widget is rendered for. |
50
|
|
|
* @param array $options The HTML attributes for the widget container tag. |
51
|
|
|
* See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
52
|
|
|
* |
53
|
|
|
* @return self |
54
|
|
|
*/ |
55
|
|
|
public function config(FormModelInterface $data, string $attribute, array $options = []): self |
56
|
|
|
{ |
57
|
|
|
$new = clone $this; |
58
|
|
|
$new->data = $data; |
59
|
|
|
$new->attribute = $attribute; |
60
|
|
|
$new->options = $options; |
61
|
|
|
return $new; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* This specifies the hint to be displayed. |
66
|
|
|
* |
67
|
|
|
* Note that this will NOT be encoded. |
68
|
|
|
* If this is not set, {@see \Yiisoft\Form\FormModel::getAttributeHint()} will be called to get the hint for |
69
|
|
|
* display (without encoding). |
70
|
|
|
* |
71
|
|
|
* @param string $value |
72
|
|
|
* |
73
|
|
|
* @return self |
74
|
|
|
*/ |
75
|
|
|
public function hint(string $value): self |
76
|
|
|
{ |
77
|
|
|
$new = clone $this; |
78
|
|
|
$new->options['hint'] = $value; |
79
|
|
|
return $new; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* The tag name of the container element. |
84
|
|
|
* |
85
|
|
|
* Null to render hint without container {@see Html::tag()}. |
86
|
|
|
* |
87
|
|
|
* @param string|null $value |
88
|
|
|
* |
89
|
|
|
* @return self |
90
|
|
|
*/ |
91
|
|
|
public function tag(?string $value = null): self |
92
|
|
|
{ |
93
|
|
|
$new = clone $this; |
94
|
|
|
$new->options['tag'] = $value; |
95
|
|
|
return $new; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|