|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Field\Base; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Form\Field\Part\Error; |
|
8
|
|
|
use Yiisoft\Form\Field\Part\Hint; |
|
9
|
|
|
use Yiisoft\Form\Field\Part\Label; |
|
10
|
|
|
use Yiisoft\Form\Helper\HtmlForm; |
|
11
|
|
|
|
|
12
|
|
|
abstract class InputField extends PartsField |
|
13
|
|
|
{ |
|
14
|
|
|
use FormAttributeTrait; |
|
15
|
|
|
|
|
16
|
|
|
protected ?string $inputId = null; |
|
17
|
|
|
protected ?string $inputIdFromTag = null; |
|
18
|
|
|
protected bool $setInputIdAttribute = true; |
|
19
|
|
|
|
|
20
|
|
|
protected array $inputTagAttributes = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Specifies the form element the tag input element belongs to. The value of this attribute must be the ID |
|
24
|
|
|
* attribute of a form element in the same document. |
|
25
|
|
|
* |
|
26
|
|
|
* @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form |
|
27
|
|
|
*/ |
|
28
|
|
|
final public function form(string $value): static |
|
29
|
|
|
{ |
|
30
|
|
|
$new = clone $this; |
|
31
|
|
|
$new->inputTagAttributes['form'] = $value; |
|
32
|
|
|
return $new; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
3 |
|
final public function inputId(?string $inputId): static |
|
36
|
|
|
{ |
|
37
|
3 |
|
$new = clone $this; |
|
38
|
3 |
|
$new->inputId = $inputId; |
|
39
|
3 |
|
return $new; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
2 |
|
final public function setInputIdAttribute(bool $value): static |
|
43
|
|
|
{ |
|
44
|
2 |
|
$new = clone $this; |
|
45
|
2 |
|
$new->setInputIdAttribute = $value; |
|
46
|
2 |
|
return $new; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
6 |
|
final public function inputTagAttributes(array $attributes): static |
|
50
|
|
|
{ |
|
51
|
6 |
|
$new = clone $this; |
|
52
|
6 |
|
$new->inputTagAttributes = $attributes; |
|
53
|
6 |
|
return $new; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
175 |
|
final protected function getInputName(): string |
|
57
|
|
|
{ |
|
58
|
175 |
|
return HtmlForm::getInputName($this->getFormModel(), $this->attribute); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
148 |
|
final protected function getInputTagAttributes(): array |
|
62
|
|
|
{ |
|
63
|
148 |
|
$attributes = $this->inputTagAttributes; |
|
64
|
|
|
|
|
65
|
148 |
|
$this->prepareIdInInputTagAttributes($attributes); |
|
66
|
|
|
|
|
67
|
148 |
|
$this->prepareInputTagAttributes($attributes); |
|
68
|
|
|
|
|
69
|
148 |
|
return $attributes; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function prepareInputTagAttributes(array &$attributes): void |
|
73
|
|
|
{ |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
148 |
|
final protected function prepareIdInInputTagAttributes(array &$attributes): void |
|
77
|
|
|
{ |
|
78
|
|
|
/** @var mixed $idFromTag */ |
|
79
|
148 |
|
$idFromTag = $attributes['id'] ?? null; |
|
80
|
148 |
|
if ($idFromTag !== null) { |
|
81
|
2 |
|
$this->inputIdFromTag = (string) $idFromTag; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
148 |
|
if ($this->setInputIdAttribute) { |
|
85
|
146 |
|
if ($this->inputId !== null) { |
|
86
|
3 |
|
$attributes['id'] = $this->inputId; |
|
87
|
143 |
|
} elseif ($idFromTag === null) { |
|
88
|
142 |
|
$attributes['id'] = $this->getInputId(); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
89 |
|
final protected function renderLabel(Label $label): string |
|
94
|
|
|
{ |
|
95
|
89 |
|
$label = $label->attribute($this->getFormModel(), $this->attribute); |
|
96
|
|
|
|
|
97
|
89 |
|
if ($this->setInputIdAttribute === false) { |
|
98
|
2 |
|
$label = $label->useInputIdAttribute(false); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
89 |
|
if ($this->inputId !== null) { |
|
102
|
3 |
|
$label = $label->forId($this->inputId); |
|
103
|
86 |
|
} elseif ($this->inputIdFromTag !== null) { |
|
104
|
1 |
|
$label = $label->forId($this->inputIdFromTag); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
89 |
|
return $label->render(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
175 |
|
final protected function renderHint(Hint $hint): string |
|
111
|
|
|
{ |
|
112
|
|
|
return $hint |
|
113
|
175 |
|
->attribute($this->getFormModel(), $this->attribute) |
|
114
|
175 |
|
->render(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
175 |
|
final protected function renderError(Error $error): string |
|
118
|
|
|
{ |
|
119
|
|
|
return $error |
|
120
|
175 |
|
->attribute($this->getFormModel(), $this->attribute) |
|
121
|
175 |
|
->render(); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|