|
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
|
67 |
|
public function run(): string |
|
24
|
|
|
{ |
|
25
|
67 |
|
$new = clone $this; |
|
26
|
|
|
|
|
27
|
67 |
|
$hint = ArrayHelper::remove($new->options, 'hint', $new->data->getAttributeHint($new->attribute)); |
|
28
|
|
|
|
|
29
|
67 |
|
if (empty($hint)) { |
|
30
|
51 |
|
return ''; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
19 |
|
$tag = ArrayHelper::remove($new->options, 'tag', 'div'); |
|
34
|
19 |
|
if (empty($tag)) { |
|
35
|
1 |
|
return $hint; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
19 |
|
$encode = ArrayHelper::remove($new->options, 'encode', true); |
|
39
|
|
|
|
|
40
|
19 |
|
return Html::tag($tag, $hint, $new->options) |
|
41
|
19 |
|
->encode($encode) |
|
42
|
19 |
|
->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
|
67 |
|
public function config(FormModelInterface $data, string $attribute, array $options = []): self |
|
56
|
|
|
{ |
|
57
|
67 |
|
$new = clone $this; |
|
58
|
67 |
|
$new->data = $data; |
|
59
|
67 |
|
$new->attribute = $attribute; |
|
60
|
67 |
|
$new->options = $options; |
|
61
|
67 |
|
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
|
1 |
|
public function hint(string $value): self |
|
76
|
|
|
{ |
|
77
|
1 |
|
$new = clone $this; |
|
78
|
1 |
|
$new->options['hint'] = $value; |
|
79
|
1 |
|
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
|
1 |
|
public function tag(?string $value = null): self |
|
92
|
|
|
{ |
|
93
|
1 |
|
$new = clone $this; |
|
94
|
1 |
|
$new->options['tag'] = $value; |
|
95
|
1 |
|
return $new; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|