|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Widget\Attribute; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
|
9
|
|
|
use Yiisoft\Form\FormModelInterface; |
|
10
|
|
|
use Yiisoft\Form\Helper\HtmlForm; |
|
11
|
|
|
|
|
12
|
|
|
trait ModelAttributes |
|
13
|
|
|
{ |
|
14
|
|
|
private array $attributes = []; |
|
15
|
|
|
private string $attribute = ''; |
|
16
|
|
|
private string $charset = 'UTF-8'; |
|
17
|
|
|
private ?string $id = ''; |
|
18
|
|
|
private ?FormModelInterface $formModel = null; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Set the character set used to generate the widget id. See {@see HtmlForm::getInputId()}. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $value |
|
24
|
|
|
* |
|
25
|
|
|
* @return static |
|
26
|
|
|
*/ |
|
27
|
1 |
|
public function charset(string $value): self |
|
28
|
|
|
{ |
|
29
|
1 |
|
$new = clone $this; |
|
30
|
1 |
|
$new->charset = $value; |
|
31
|
1 |
|
return $new; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Set form interface, attribute name and attributes, and attributes for the widget. |
|
36
|
|
|
* |
|
37
|
|
|
* @param FormModelInterface $formModel Form. |
|
38
|
|
|
* @param string $attribute Form model property this widget is rendered for. |
|
39
|
|
|
* @param array $attributes The HTML attributes for the widget container tag. |
|
40
|
|
|
* |
|
41
|
|
|
* @return static |
|
42
|
|
|
* |
|
43
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
|
44
|
|
|
*/ |
|
45
|
316 |
|
public function config(FormModelInterface $formModel, string $attribute, array $attributes = []): self |
|
46
|
|
|
{ |
|
47
|
316 |
|
$new = clone $this; |
|
48
|
316 |
|
$new->formModel = $formModel; |
|
49
|
316 |
|
$new->attribute = $attribute; |
|
50
|
316 |
|
$new->attributes = $attributes; |
|
51
|
316 |
|
return $new; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Set the ID of the widget. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string|null $value The ID of the widget. if `null` the attribute will be removed. |
|
58
|
|
|
* |
|
59
|
|
|
* @return static |
|
60
|
|
|
* |
|
61
|
|
|
* @link https://html.spec.whatwg.org/multipage/dom.html#the-id-attribute |
|
62
|
|
|
*/ |
|
63
|
1 |
|
public function id(?string $value): self |
|
64
|
|
|
{ |
|
65
|
1 |
|
$new = clone $this; |
|
66
|
1 |
|
$new->id = $value; |
|
67
|
1 |
|
return $new; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
313 |
|
protected function getFormModel(): FormModelInterface |
|
71
|
|
|
{ |
|
72
|
313 |
|
if ($this->formModel === null) { |
|
73
|
1 |
|
throw new InvalidArgumentException('Form model is not set.'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
312 |
|
return $this->formModel; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Generates a unique ID for the attribute. |
|
81
|
|
|
* |
|
82
|
|
|
* @return string|null |
|
83
|
|
|
*/ |
|
84
|
268 |
|
protected function generateId(): ?string |
|
85
|
|
|
{ |
|
86
|
268 |
|
$new = clone $this; |
|
87
|
|
|
|
|
88
|
|
|
/** @var string */ |
|
89
|
268 |
|
$id = ArrayHelper::remove($new->attributes, 'id', $new->id); |
|
90
|
|
|
|
|
91
|
268 |
|
return $id === '' ? HtmlForm::getInputId($new->getFormModel(), $new->attribute, $new->charset) : $id; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|