|
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
|
|
|
|
|
11
|
|
|
abstract class PartsField extends BaseField |
|
12
|
|
|
{ |
|
13
|
|
|
protected string $templateBegin = "{label}\n{input}"; |
|
14
|
|
|
protected string $templateEnd = "{input}\n{hint}\n{error}"; |
|
15
|
|
|
protected string $template = "{label}\n{input}\n{hint}\n{error}"; |
|
16
|
|
|
protected ?bool $hideLabel = null; |
|
17
|
|
|
|
|
18
|
|
|
private array $labelConfig = []; |
|
19
|
|
|
private array $hintConfig = []; |
|
20
|
|
|
private array $errorConfig = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Set layout template for render a field. |
|
24
|
|
|
*/ |
|
25
|
2 |
|
final public function template(string $template): static |
|
26
|
|
|
{ |
|
27
|
2 |
|
$new = clone $this; |
|
28
|
2 |
|
$new->template = $template; |
|
29
|
2 |
|
return $new; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
2 |
|
final public function hideLabel(?bool $hide = true): static |
|
33
|
|
|
{ |
|
34
|
2 |
|
$new = clone $this; |
|
35
|
2 |
|
$new->hideLabel = $hide; |
|
36
|
2 |
|
return $new; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
3 |
|
final public function labelConfig(array $config): static |
|
40
|
|
|
{ |
|
41
|
3 |
|
$new = clone $this; |
|
42
|
3 |
|
$new->labelConfig = $config; |
|
43
|
3 |
|
return $new; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
2 |
|
final public function label(?string $content): static |
|
47
|
|
|
{ |
|
48
|
2 |
|
$new = clone $this; |
|
49
|
2 |
|
$new->labelConfig['content()'] = [$content]; |
|
50
|
2 |
|
return $new; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
2 |
|
final public function hintConfig(array $config): static |
|
54
|
|
|
{ |
|
55
|
2 |
|
$new = clone $this; |
|
56
|
2 |
|
$new->hintConfig = $config; |
|
57
|
2 |
|
return $new; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
2 |
|
final public function hint(?string $content): static |
|
61
|
|
|
{ |
|
62
|
2 |
|
$new = clone $this; |
|
63
|
2 |
|
$new->hintConfig['content()'] = [$content]; |
|
64
|
2 |
|
return $new; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
final public function errorConfig(array $config): static |
|
68
|
|
|
{ |
|
69
|
2 |
|
$new = clone $this; |
|
70
|
2 |
|
$new->errorConfig = $config; |
|
71
|
2 |
|
return $new; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
final public function error(?string $message): static |
|
75
|
|
|
{ |
|
76
|
1 |
|
$new = clone $this; |
|
77
|
1 |
|
$new->errorConfig['message()'] = [$message]; |
|
78
|
1 |
|
return $new; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
79 |
|
protected function shouldHideLabel(): bool |
|
82
|
|
|
{ |
|
83
|
79 |
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function generateInput(): string |
|
87
|
|
|
{ |
|
88
|
|
|
return ''; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
protected function generateBeginInput(): string |
|
92
|
|
|
{ |
|
93
|
|
|
return ''; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
protected function generateEndInput(): string |
|
97
|
|
|
{ |
|
98
|
|
|
return ''; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
18 |
|
protected function renderLabel(Label $label): string |
|
102
|
|
|
{ |
|
103
|
18 |
|
return $label->render(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
18 |
|
protected function renderHint(Hint $hint): string |
|
107
|
|
|
{ |
|
108
|
18 |
|
return $hint->render(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
18 |
|
protected function renderError(Error $error): string |
|
112
|
|
|
{ |
|
113
|
18 |
|
return $error->render(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
100 |
|
final protected function generateContent(): ?string |
|
117
|
|
|
{ |
|
118
|
99 |
|
$parts = [ |
|
119
|
100 |
|
'{input}' => $this->generateInput(), |
|
120
|
99 |
|
'{label}' => ($this->hideLabel ?? $this->shouldHideLabel()) ? '' : $this->generateLabel(), |
|
121
|
99 |
|
'{hint}' => $this->generateHint(), |
|
122
|
99 |
|
'{error}' => $this->generateError(), |
|
123
|
|
|
]; |
|
124
|
|
|
|
|
125
|
99 |
|
return preg_replace('/^\h*\v+/m', '', trim(strtr($this->template, $parts))); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
1 |
|
final protected function generateBeginContent(): string |
|
129
|
|
|
{ |
|
130
|
1 |
|
$parts = [ |
|
131
|
1 |
|
'{input}' => $this->generateBeginInput(), |
|
132
|
1 |
|
'{label}' => ($this->hideLabel ?? $this->shouldHideLabel()) ? '' : $this->generateLabel(), |
|
133
|
1 |
|
'{hint}' => $this->generateHint(), |
|
134
|
1 |
|
'{error}' => $this->generateError(), |
|
135
|
|
|
]; |
|
136
|
|
|
|
|
137
|
1 |
|
return preg_replace('/^\h*\v+/m', '', trim(strtr($this->templateBegin, $parts))); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
1 |
|
final protected function generateEndContent(): string |
|
141
|
|
|
{ |
|
142
|
1 |
|
$parts = [ |
|
143
|
1 |
|
'{input}' => $this->generateEndInput(), |
|
144
|
1 |
|
'{label}' => ($this->hideLabel ?? $this->shouldHideLabel()) ? '' : $this->generateLabel(), |
|
145
|
1 |
|
'{hint}' => $this->generateHint(), |
|
146
|
1 |
|
'{error}' => $this->generateError(), |
|
147
|
|
|
]; |
|
148
|
|
|
|
|
149
|
1 |
|
return preg_replace('/^\h*\v+/m', '', trim(strtr($this->templateEnd, $parts))); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
84 |
|
private function generateLabel(): string |
|
153
|
|
|
{ |
|
154
|
84 |
|
$label = Label::widget($this->labelConfig); |
|
155
|
|
|
|
|
156
|
84 |
|
return $this->renderLabel($label); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
99 |
|
private function generateHint(): string |
|
160
|
|
|
{ |
|
161
|
99 |
|
$hint = Hint::widget($this->hintConfig); |
|
162
|
|
|
|
|
163
|
99 |
|
return $this->renderHint($hint); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
99 |
|
private function generateError(): string |
|
167
|
|
|
{ |
|
168
|
99 |
|
$error = Error::widget($this->errorConfig); |
|
169
|
|
|
|
|
170
|
99 |
|
return $this->renderError($error); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|