1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Widget; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
8
|
|
|
use Yiisoft\Definitions\Exception\CircularReferenceException; |
9
|
|
|
use Yiisoft\Definitions\Exception\InvalidConfigException; |
10
|
|
|
use Yiisoft\Definitions\Exception\NotInstantiableException; |
11
|
|
|
use Yiisoft\Factory\NotFoundException; |
12
|
|
|
use Yiisoft\Form\FormModelInterface; |
13
|
|
|
use Yiisoft\Form\Widget\Attribute\ButtonAttributes; |
14
|
|
|
use Yiisoft\Form\Widget\Attribute\FieldAttributes; |
15
|
|
|
use Yiisoft\Form\Widget\Attribute\InputAttributes; |
16
|
|
|
use Yiisoft\Form\Widget\Attribute\GlobalAttributes; |
17
|
|
|
use Yiisoft\Form\Widget\Attribute\PlaceholderInterface; |
18
|
|
|
use Yiisoft\Form\Widget\Attribute\WidgetAttributes; |
19
|
|
|
use Yiisoft\Form\Widget\FieldPart\Error; |
20
|
|
|
use Yiisoft\Form\Widget\FieldPart\Hint; |
21
|
|
|
use Yiisoft\Form\Widget\FieldPart\Label; |
22
|
|
|
use Yiisoft\Html\Html; |
23
|
|
|
use Yiisoft\Html\Tag\Div; |
24
|
|
|
|
25
|
|
|
use function strtr; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Renders the field widget along with label and hint tag (if any) according to template. |
29
|
|
|
* |
30
|
|
|
* @psalm-suppress MissingConstructor |
31
|
|
|
*/ |
32
|
|
|
final class Field extends FieldAttributes |
33
|
|
|
{ |
34
|
|
|
/** @psalm-var ButtonAttributes[] */ |
35
|
|
|
private array $buttons = []; |
36
|
|
|
private WidgetAttributes $inputWidget; |
37
|
|
|
private GlobalAttributes $widget; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Renders a checkbox. |
41
|
|
|
* |
42
|
|
|
* This method will generate the `checked` tag attribute according to the model attribute value. |
43
|
|
|
* |
44
|
|
|
* @param FormModelInterface $formModel The model object. |
45
|
|
|
* @param string $attribute The attribute name or expression. |
46
|
|
|
* @param array $config The configuration array for widget factory. |
47
|
|
|
* Available methods: |
48
|
|
|
* [ |
49
|
|
|
* 'enclosedByLabel()' => [false], |
50
|
|
|
* 'label()' => ['test-text-label'], |
51
|
|
|
* 'labelAttributes()' => [['class' => 'test-class']], |
52
|
|
|
* 'uncheckValue()' => ['0'], |
53
|
|
|
* ] |
54
|
|
|
* |
55
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
56
|
|
|
* |
57
|
|
|
* @return static the field widget instance. |
58
|
|
|
*/ |
59
|
20 |
|
public function checkbox(FormModelInterface $formModel, string $attribute, array $config = []): self |
60
|
|
|
{ |
61
|
20 |
|
$new = clone $this; |
62
|
|
|
|
63
|
|
|
/** @var array */ |
64
|
20 |
|
$enclosedByLabel = $config['enclosedByLabel()'] ?? [true]; |
65
|
|
|
|
66
|
20 |
|
if ($enclosedByLabel === [true]) { |
67
|
19 |
|
$new->parts['{label}'] = ''; |
68
|
|
|
} |
69
|
|
|
|
70
|
20 |
|
$new->inputWidget = Checkbox::widget($config)->for($formModel, $attribute); |
|
|
|
|
71
|
20 |
|
return $new; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Renders a list of checkboxes. |
76
|
|
|
* |
77
|
|
|
* A checkbox list allows multiple selection, As a result, the corresponding submitted value is an array. |
78
|
|
|
* |
79
|
|
|
* The selection of the checkbox list is taken from the value of the model attribute. |
80
|
|
|
* |
81
|
|
|
* @param FormModelInterface $formModel The model object. |
82
|
|
|
* @param string $attribute The attribute name or expression. |
83
|
|
|
* @param array $config the configuration array for widget factory. |
84
|
|
|
* Available methods: |
85
|
|
|
* [ |
86
|
|
|
* 'containerAttributes()' => [['class' => 'test-class']], |
87
|
|
|
* 'containerTag()' => ['span'], |
88
|
|
|
* 'individualItemsAttributes()' => [[1 => ['disabled' => true], 2 => ['class' => 'test-class']], |
89
|
|
|
* 'items()' => [[1 => 'Female', 2 => 'Male']], |
90
|
|
|
* 'itemsAttributes()' => [['disabled' => true], |
91
|
|
|
* 'itemsFormatter()' => [ |
92
|
|
|
* static function (CheckboxItem $item) { |
93
|
|
|
* return $item->checked |
94
|
|
|
* ? "<label><input type='checkbox' name='$item->name' value='$item->value' checked> $item->label</label>" |
95
|
|
|
* : "<label><input type='checkbox' name='$item->name' value='$item->value'> $item->label</label>"; |
96
|
|
|
* }, |
97
|
|
|
* ], |
98
|
|
|
* 'itemsFromValues()' => [[1 => 'Female', 2 => 'Male']], |
99
|
|
|
* 'separator()' => ['⚊'], |
100
|
|
|
* ] |
101
|
|
|
* |
102
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
103
|
|
|
* |
104
|
|
|
* @return static the field widget instance. |
105
|
|
|
*/ |
106
|
19 |
|
public function checkboxList(FormModelInterface $formModel, string $attribute, array $config = []): self |
107
|
|
|
{ |
108
|
19 |
|
$new = clone $this; |
109
|
19 |
|
$new->inputWidget = CheckboxList::widget($config)->for($formModel, $attribute); |
110
|
19 |
|
return $new; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Renders a date widget. |
115
|
|
|
* |
116
|
|
|
* @param FormModelInterface $formModel The model object. |
117
|
|
|
* @param string $attribute The attribute name or expression. |
118
|
|
|
* @param array $config the configuration array for widget factory. |
119
|
|
|
* |
120
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
121
|
|
|
* |
122
|
|
|
* @return static the field widget instance. |
123
|
|
|
*/ |
124
|
15 |
|
public function date(FormModelInterface $formModel, string $attribute, array $config = []): self |
125
|
|
|
{ |
126
|
15 |
|
$new = clone $this; |
127
|
15 |
|
$new->inputWidget = Date::widget($config)->for($formModel, $attribute); |
128
|
15 |
|
return $new; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Renders a date time widget. |
133
|
|
|
* |
134
|
|
|
* @param FormModelInterface $formModel The model object. |
135
|
|
|
* @param string $attribute The attribute name or expression. |
136
|
|
|
* @param array $config the configuration array for widget factory. |
137
|
|
|
* |
138
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
139
|
|
|
* |
140
|
|
|
* @return static the field widget instance. |
141
|
|
|
*/ |
142
|
16 |
|
public function dateTime(FormModelInterface $formModel, string $attribute, array $config = []): self |
143
|
|
|
{ |
144
|
16 |
|
$new = clone $this; |
145
|
16 |
|
$new->inputWidget = DateTime::widget($config)->for($formModel, $attribute); |
146
|
16 |
|
return $new; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Renders a date time local widget. |
151
|
|
|
* |
152
|
|
|
* @param FormModelInterface $formModel The model object. |
153
|
|
|
* @param string $attribute The attribute name or expression. |
154
|
|
|
* @param array $config the configuration array for widget factory. |
155
|
|
|
* |
156
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
157
|
|
|
* |
158
|
|
|
* @return static the field widget instance. |
159
|
|
|
*/ |
160
|
16 |
|
public function dateTimeLocal(FormModelInterface $formModel, string $attribute, array $config = []): self |
161
|
|
|
{ |
162
|
16 |
|
$new = clone $this; |
163
|
16 |
|
$new->inputWidget = DateTimeLocal::widget($config)->for($formModel, $attribute); |
164
|
16 |
|
return $new; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Renders a email widget. |
169
|
|
|
* |
170
|
|
|
* @param FormModelInterface $formModel The model object. |
171
|
|
|
* @param string $attribute The attribute name or expression. |
172
|
|
|
* @param array $config the configuration array for widget factory. |
173
|
|
|
* |
174
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
175
|
|
|
* |
176
|
|
|
* @return static the field widget instance. |
177
|
|
|
*/ |
178
|
23 |
|
public function email(FormModelInterface $formModel, string $attribute, array $config = []): self |
179
|
|
|
{ |
180
|
23 |
|
$new = clone $this; |
181
|
23 |
|
$new->inputWidget = Email::widget($config)->for($formModel, $attribute); |
182
|
23 |
|
return $new; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Renders a file widget. |
187
|
|
|
* |
188
|
|
|
* @param FormModelInterface $formModel The model object. |
189
|
|
|
* @param string $attribute The attribute name or expression. |
190
|
|
|
* @param array $config the configuration array for widget factory. |
191
|
|
|
* Available methods: |
192
|
|
|
* [ |
193
|
|
|
* 'hiddenAttributes()' => [['id' => 'test-id']], |
194
|
|
|
* 'uncheckValue()' => ['0'], |
195
|
|
|
* |
196
|
|
|
* ] |
197
|
|
|
* |
198
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
199
|
|
|
* |
200
|
|
|
* @return static the field widget instance. |
201
|
|
|
*/ |
202
|
14 |
|
public function file(FormModelInterface $formModel, string $attribute, array $config = []): self |
203
|
|
|
{ |
204
|
14 |
|
$new = clone $this; |
205
|
14 |
|
$new->inputWidget = File::widget($config)->for($formModel, $attribute); |
206
|
14 |
|
return $new; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Renders a hidden widget. |
211
|
|
|
* |
212
|
|
|
* @param FormModelInterface $formModel The model object. |
213
|
|
|
* @param string $attribute The attribute name or expression. |
214
|
|
|
* @param array $config the configuration array for widget factory. |
215
|
|
|
* |
216
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
217
|
|
|
* |
218
|
|
|
* @return static the field widget instance. |
219
|
|
|
*/ |
220
|
2 |
|
public function hidden(FormModelInterface $formModel, string $attribute, array $config = []): self |
221
|
|
|
{ |
222
|
2 |
|
$new = clone $this; |
223
|
2 |
|
$new->parts['{label}'] = ''; |
224
|
2 |
|
$new->parts['{hint}'] = ''; |
225
|
2 |
|
$new->parts['{error}'] = ''; |
226
|
2 |
|
$new->inputWidget = Hidden::widget($config)->for($formModel, $attribute); |
227
|
2 |
|
return $new; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Renders an image widget. |
232
|
|
|
* |
233
|
|
|
* @param array $config the configuration array for widget factory. |
234
|
|
|
* @param array $attributes the HTML attributes for the widget. |
235
|
|
|
* Most used attributes: |
236
|
|
|
* [ |
237
|
|
|
* 'alt' => 'test-alt', |
238
|
|
|
* 'height' => '100%', |
239
|
|
|
* 'src' => 'test-src', |
240
|
|
|
* 'width' => '100%', |
241
|
|
|
* ] |
242
|
|
|
* |
243
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
244
|
|
|
* |
245
|
|
|
* @return static the field object itself. |
246
|
|
|
*/ |
247
|
12 |
|
public function image(array $config = [], array $attributes = []): self |
248
|
|
|
{ |
249
|
12 |
|
$new = clone $this; |
250
|
12 |
|
$new->parts['{label}'] = ''; |
251
|
12 |
|
$new->parts['{hint}'] = ''; |
252
|
12 |
|
$new->parts['{error}'] = ''; |
253
|
12 |
|
$new->widget = Image::widget($config)->attributes($attributes); |
254
|
12 |
|
return $new; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Renders a number widget. |
259
|
|
|
* |
260
|
|
|
* @param FormModelInterface $formModel The model object. |
261
|
|
|
* @param string $attribute The attribute name or expression. |
262
|
|
|
* @param array $config the configuration array for widget factory. |
263
|
|
|
* |
264
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
265
|
|
|
* |
266
|
|
|
* @return static the field object itself. |
267
|
|
|
*/ |
268
|
18 |
|
public function number(FormModelInterface $formModel, string $attribute, array $config = []): self |
269
|
|
|
{ |
270
|
18 |
|
$new = clone $this; |
271
|
18 |
|
$new->inputWidget = Number::widget($config)->for($formModel, $attribute); |
272
|
18 |
|
return $new; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Renders a password widget. |
277
|
|
|
* |
278
|
|
|
* @param FormModelInterface $formModel The model object. |
279
|
|
|
* @param string $attribute The attribute name or expression. |
280
|
|
|
* @param array $config the configuration array for widget factory. |
281
|
|
|
* |
282
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
283
|
|
|
* |
284
|
|
|
* @return static the field object itself. |
285
|
|
|
*/ |
286
|
21 |
|
public function password(FormModelInterface $formModel, string $attribute, array $config = []): self |
287
|
|
|
{ |
288
|
21 |
|
$new = clone $this; |
289
|
21 |
|
$new->inputWidget = Password::widget($config)->for($formModel, $attribute); |
290
|
21 |
|
return $new; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Renders a radio widget. |
295
|
|
|
* |
296
|
|
|
* @param FormModelInterface $formModel The model object. |
297
|
|
|
* @param string $attribute The attribute name or expression. |
298
|
|
|
* @param array $config the configuration array for widget factory. |
299
|
|
|
* Available methods: |
300
|
|
|
* [ |
301
|
|
|
* 'enclosedByLabel()' => [false], |
302
|
|
|
* 'label()' => ['Email:'], |
303
|
|
|
* 'labelAttributes()' => [['class' => 'test-class']] |
304
|
|
|
* 'uncheckValue()' => ['0'], |
305
|
|
|
* ] |
306
|
|
|
* |
307
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
308
|
|
|
* |
309
|
|
|
* @return static the field object itself. |
310
|
|
|
*/ |
311
|
21 |
|
public function radio(FormModelInterface $formModel, string $attribute, array $config = []): self |
312
|
|
|
{ |
313
|
21 |
|
$new = clone $this; |
314
|
|
|
|
315
|
|
|
/** @var array */ |
316
|
21 |
|
$enclosedByLabel = $config['enclosedByLabel()'] ?? [true]; |
317
|
|
|
|
318
|
21 |
|
if ($enclosedByLabel === [true]) { |
319
|
20 |
|
$new->parts['{label}'] = ''; |
320
|
|
|
} |
321
|
|
|
|
322
|
21 |
|
$new->inputWidget = Radio::widget($config)->for($formModel, $attribute); |
323
|
21 |
|
return $new; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Renders a radio list widget. |
328
|
|
|
* |
329
|
|
|
* @param FormModelInterface $formModel The model object. |
330
|
|
|
* @param string $attribute The attribute name or expression. |
331
|
|
|
* @param array $config the configuration array for widget factory. |
332
|
|
|
* Available methods: |
333
|
|
|
* [ |
334
|
|
|
* 'containerAttributes()' => [['class' => 'test-class']], |
335
|
|
|
* 'containerTag()' => ['span'], |
336
|
|
|
* 'items()' => [[1 => 'Female', 2 => 'Male']], |
337
|
|
|
* 'itemsAttributes()' => [['class' => 'test-class']], |
338
|
|
|
* 'individualItemsAttributes()' => [[1 => ['disabled' => true], 2 => ['class' => 'test-class']]], |
339
|
|
|
* 'itemsFormatter()' => [ |
340
|
|
|
* static function (RadioItem $item) { |
341
|
|
|
* return $item->checked |
342
|
|
|
* ? "<label><input type='checkbox' name='$item->name' value='$item->value' checked> $item->label</label>" |
343
|
|
|
* : "<label><input type='checkbox' name='$item->name' value='$item->value'> $item->label</label>"; |
344
|
|
|
* }, |
345
|
|
|
* ], |
346
|
|
|
* 'itemsFromValues()' => [[1 => 'Female', 2 => 'Male']], |
347
|
|
|
* 'separator()' => [PHP_EOL], |
348
|
|
|
* 'uncheckValue()' => ['0'], |
349
|
|
|
* ] |
350
|
|
|
* |
351
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
352
|
|
|
* |
353
|
|
|
* @return static the field object itself. |
354
|
|
|
*/ |
355
|
20 |
|
public function radioList(FormModelInterface $formModel, string $attribute, array $config = []): self |
356
|
|
|
{ |
357
|
20 |
|
$new = clone $this; |
358
|
20 |
|
$new->inputWidget = RadioList::widget($config)->for($formModel, $attribute); |
359
|
20 |
|
return $new; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Renders a range widget. |
364
|
|
|
* |
365
|
|
|
* @param FormModelInterface $formModel The model object. |
366
|
|
|
* @param string $attribute The attribute name or expression. |
367
|
|
|
* @param array $config the configuration array for widget factory. |
368
|
|
|
* Available methods: |
369
|
|
|
* [ |
370
|
|
|
* 'outputTag()' => ['p'], |
371
|
|
|
* 'outputAttributes()' => [['class' => 'test-class']], |
372
|
|
|
* ] |
373
|
|
|
* |
374
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
375
|
|
|
* |
376
|
|
|
* @return static the field object itself. |
377
|
|
|
*/ |
378
|
19 |
|
public function range(FormModelInterface $formModel, string $attribute, array $config = []): self |
379
|
|
|
{ |
380
|
19 |
|
$new = clone $this; |
381
|
19 |
|
$new->inputWidget = Range::widget($config)->for($formModel, $attribute); |
382
|
19 |
|
return $new; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Renders a reset button widget. |
387
|
|
|
* |
388
|
|
|
* @param array $config the configuration array for widget factory. |
389
|
|
|
* @param array $attributes the HTML attributes for the widget. |
390
|
|
|
* |
391
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
392
|
|
|
* |
393
|
|
|
* @return static the field object itself. |
394
|
|
|
*/ |
395
|
10 |
|
public function resetButton(array $config = [], array $attributes = []): self |
396
|
|
|
{ |
397
|
10 |
|
$new = clone $this; |
398
|
10 |
|
$new->buttons[] = ResetButton::widget($config)->attributes($attributes); |
399
|
10 |
|
return $new; |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Renders a select widget. |
404
|
|
|
* |
405
|
|
|
* @param FormModelInterface $formModel The model object. |
406
|
|
|
* @param string $attribute The attribute name or expression. |
407
|
|
|
* @param array $config The configuration array for widget factory. |
408
|
|
|
* Available methods: |
409
|
|
|
* [ |
410
|
|
|
* 'encode()' => [true], |
411
|
|
|
* 'groups()' => [['1' => ['2' => 'Moscu', '3' => 'San Petersburg']]], |
412
|
|
|
* 'items()' => [['1' => 'Moscu', '2' => 'San Petersburg']], |
413
|
|
|
* 'itemsAttributes()' => [['2' => ['disabled' => true]], |
414
|
|
|
* 'optionsData()' => [['1' => '<b>Moscu</b>', '2' => 'San Petersburg']], |
415
|
|
|
* 'prompt()' => [['text' => 'Select City Birth', 'attributes' => ['value' => '0', 'selected' => 'selected']]], |
416
|
|
|
* 'unselectValue()' => ['0'], |
417
|
|
|
* ] |
418
|
|
|
* |
419
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
420
|
|
|
* |
421
|
|
|
* @return static the field object itself. |
422
|
|
|
*/ |
423
|
21 |
|
public function select(FormModelInterface $formModel, string $attribute, array $config = []): self |
424
|
|
|
{ |
425
|
21 |
|
$new = clone $this; |
426
|
21 |
|
$new->inputWidget = Select::widget($config)->for($formModel, $attribute); |
427
|
21 |
|
return $new; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Renders a submit button widget. |
432
|
|
|
* |
433
|
|
|
* @param array $config the configuration array for widget factory. |
434
|
|
|
* @param array $attributes the HTML attributes for the widget. |
435
|
|
|
* |
436
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
437
|
|
|
* |
438
|
|
|
* @return static the field object itself. |
439
|
|
|
*/ |
440
|
12 |
|
public function submitButton(array $config = [], array $attributes = []): self |
441
|
|
|
{ |
442
|
12 |
|
$new = clone $this; |
443
|
12 |
|
$new->buttons[] = SubmitButton::widget($config)->attributes($attributes); |
444
|
12 |
|
return $new; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* Renders a text widget. |
449
|
|
|
* |
450
|
|
|
* @param FormModelInterface $formModel The model object. |
451
|
|
|
* @param string $attribute The attribute name or expression. |
452
|
|
|
* @param array $config the configuration array for widget factory. |
453
|
|
|
* |
454
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
455
|
|
|
* |
456
|
|
|
* @return static the field widget instance. |
457
|
|
|
*/ |
458
|
21 |
|
public function telephone(FormModelInterface $formModel, string $attribute, array $config = []): self |
459
|
|
|
{ |
460
|
21 |
|
$new = clone $this; |
461
|
21 |
|
$new->inputWidget = Telephone::widget($config)->for($formModel, $attribute); |
462
|
21 |
|
return $new; |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* Renders a text widget. |
467
|
|
|
* |
468
|
|
|
* @param FormModelInterface $formModel The model object. |
469
|
|
|
* @param string $attribute The attribute name or expression. |
470
|
|
|
* @param array $config the configuration array for widget factory. |
471
|
|
|
* |
472
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
473
|
|
|
* |
474
|
|
|
* @return static the field widget instance. |
475
|
|
|
*/ |
476
|
43 |
|
public function text(FormModelInterface $formModel, string $attribute, array $config = []): self |
477
|
|
|
{ |
478
|
43 |
|
$new = clone $this; |
479
|
43 |
|
$new->inputWidget = Text::widget($config)->for($formModel, $attribute); |
480
|
42 |
|
return $new; |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Renders a text area widget. |
485
|
|
|
* |
486
|
|
|
* @param FormModelInterface $formModel The model object. |
487
|
|
|
* @param string $attribute The attribute name or expression. |
488
|
|
|
* @param array $config the configuration array for widget factory. |
489
|
|
|
* |
490
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
491
|
|
|
* |
492
|
|
|
* @return static the field widget instance. |
493
|
|
|
*/ |
494
|
24 |
|
public function textArea(FormModelInterface $formModel, string $attribute, array $config = []): self |
495
|
|
|
{ |
496
|
24 |
|
$new = clone $this; |
497
|
24 |
|
$new->inputWidget = TextArea::widget($config)->for($formModel, $attribute); |
498
|
22 |
|
return $new; |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* Renders a url widget. |
503
|
|
|
* |
504
|
|
|
* @param FormModelInterface $formModel The model object. |
505
|
|
|
* @param string $attribute The attribute name or expression. |
506
|
|
|
* @param array $config the configuration array for widget factory. |
507
|
|
|
* |
508
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
509
|
|
|
* |
510
|
|
|
* @return static the field widget instance. |
511
|
|
|
*/ |
512
|
23 |
|
public function url(FormModelInterface $formModel, string $attribute, array $config = []): self |
513
|
|
|
{ |
514
|
23 |
|
$new = clone $this; |
515
|
23 |
|
$new->inputWidget = Url::widget($config)->for($formModel, $attribute); |
516
|
23 |
|
return $new; |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
/** |
520
|
|
|
* Renders the whole field. |
521
|
|
|
* |
522
|
|
|
* This method will generate the label, input tag and hint tag (if any), and assemble them into HTML according to |
523
|
|
|
* {@see template}. |
524
|
|
|
* |
525
|
|
|
* If (not set), the default methods will be called to generate the label and input tag, and use them as the |
526
|
|
|
* content. |
527
|
|
|
* |
528
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
529
|
|
|
* |
530
|
|
|
* @return string the rendering result. |
531
|
|
|
*/ |
532
|
386 |
|
protected function run(): string |
533
|
|
|
{ |
534
|
386 |
|
$content = ''; |
535
|
|
|
|
536
|
386 |
|
$div = Div::tag(); |
537
|
|
|
|
538
|
386 |
|
if ($this->getContainerClass() !== '') { |
539
|
|
|
$div = $div->class($this->getContainerClass()); |
540
|
|
|
} |
541
|
|
|
|
542
|
386 |
|
if ($this->getContainerAttributes() !== []) { |
543
|
3 |
|
$div = $div->attributes($this->getContainerAttributes()); |
544
|
|
|
} |
545
|
|
|
|
546
|
386 |
|
if (!empty($this->inputWidget)) { |
547
|
353 |
|
$content .= $this->renderInputWidget(); |
548
|
|
|
} |
549
|
|
|
|
550
|
368 |
|
if (!empty($this->widget)) { |
551
|
12 |
|
$content .= $this->widget->attributes($this->attributes)->render(); |
552
|
|
|
} |
553
|
|
|
|
554
|
368 |
|
$renderButtons = $this->renderButtons(); |
555
|
|
|
|
556
|
368 |
|
if ($renderButtons !== '') { |
557
|
21 |
|
$content .= $renderButtons; |
558
|
|
|
} |
559
|
|
|
|
560
|
368 |
|
return $this->getContainer() ? $div->content(PHP_EOL . $content . PHP_EOL)->encode(false)->render() : $content; |
561
|
|
|
} |
562
|
|
|
|
563
|
353 |
|
private function buildField(): self |
564
|
|
|
{ |
565
|
353 |
|
$new = clone $this; |
566
|
|
|
|
567
|
|
|
// Set ariadescribedby. |
568
|
353 |
|
if ($new->getAriaDescribedBy() === true && $new->inputWidget instanceof InputAttributes) { |
569
|
|
|
$new->inputWidget = $new->inputWidget->ariaDescribedBy($new->inputWidget->getAttribute() . 'Help'); |
|
|
|
|
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
// Set encode. |
573
|
353 |
|
$new->inputWidget = $new->inputWidget->encode($new->getEncode()); |
574
|
|
|
|
575
|
|
|
// Set input class. |
576
|
353 |
|
if ($new->inputClass !== '') { |
577
|
|
|
$new->inputWidget = $new->inputWidget->class($new->inputClass); |
578
|
|
|
} |
579
|
|
|
|
580
|
|
|
// Set placeholder. |
581
|
353 |
|
$new->placeholder ??= $new->inputWidget->getAttributePlaceHolder(); |
582
|
|
|
|
583
|
353 |
|
if ($new->inputWidget instanceof PlaceholderInterface && $new->placeholder !== '') { |
584
|
7 |
|
$new->inputWidget = $new->inputWidget->attributes(['placeholder' => $new->placeholder]); |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
// Set valid class and invalid class. |
588
|
353 |
|
if ($new->invalidClass !== '' && $new->inputWidget->hasError()) { |
|
|
|
|
589
|
2 |
|
$new->inputWidget = $new->inputWidget->class($new->invalidClass); |
590
|
351 |
|
} elseif ($new->validClass !== '' && $new->inputWidget->isValidated()) { |
|
|
|
|
591
|
2 |
|
$new->inputWidget = $new->inputWidget->class($new->validClass); |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
// Set attributes. |
595
|
353 |
|
$new->inputWidget = $new->inputWidget->attributes($this->attributes); |
596
|
|
|
|
597
|
353 |
|
return $new; |
598
|
|
|
} |
599
|
|
|
|
600
|
368 |
|
private function renderButtons(): string |
601
|
|
|
{ |
602
|
368 |
|
$buttons = ''; |
603
|
|
|
|
604
|
368 |
|
foreach ($this->buttons as $key => $button) { |
605
|
21 |
|
$buttonsAttributes = $this->getButtonsIndividualAttributes((string) $key) ?? $this->attributes; |
606
|
|
|
|
607
|
|
|
// Set input class. |
608
|
21 |
|
if ($this->inputClass !== '') { |
609
|
|
|
$button = $button->class($this->inputClass); |
610
|
|
|
} |
611
|
|
|
|
612
|
21 |
|
$buttons .= $button->attributes($buttonsAttributes)->render(); |
613
|
|
|
} |
614
|
|
|
|
615
|
368 |
|
return $buttons; |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
/** |
619
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
620
|
|
|
*/ |
621
|
333 |
|
private function renderError(): string |
622
|
|
|
{ |
623
|
333 |
|
$errorAttributes = $this->getErrorAttributes(); |
624
|
333 |
|
$errorClass = $this->getErrorIndividualClass($this->type) ?? $this->getErrorClass(); |
625
|
|
|
|
626
|
333 |
|
if ($errorClass !== '') { |
627
|
3 |
|
Html::addCssClass($errorAttributes, $errorClass); |
628
|
|
|
} |
629
|
|
|
|
630
|
333 |
|
return Error::widget() |
631
|
333 |
|
->attributes($errorAttributes) |
632
|
333 |
|
->encode($this->getEncode()) |
633
|
333 |
|
->for($this->inputWidget->getFormModel(), $this->inputWidget->getAttribute()) |
634
|
333 |
|
->message($this->getError() ?? '') |
635
|
333 |
|
->messageCallback($this->getErrorMessageCallback()) |
636
|
333 |
|
->tag($this->getErrorTag()) |
637
|
333 |
|
->render(); |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
/** |
641
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
642
|
|
|
*/ |
643
|
353 |
|
private function renderInputWidget(): string |
644
|
|
|
{ |
645
|
353 |
|
$new = clone $this; |
646
|
|
|
|
647
|
353 |
|
$new = $new->buildField(); |
648
|
|
|
|
649
|
353 |
|
if (!array_key_exists('{input}', $new->parts)) { |
650
|
353 |
|
$new->parts['{input}'] = $new->inputWidget->render(); |
651
|
|
|
} |
652
|
|
|
|
653
|
335 |
|
if (!array_key_exists('{error}', $new->parts)) { |
654
|
334 |
|
$new->parts['{error}'] = $this->getError() !== null ? $new->renderError() : ''; |
655
|
|
|
} |
656
|
|
|
|
657
|
335 |
|
if (!array_key_exists('{hint}', $new->parts)) { |
658
|
334 |
|
$new->parts['{hint}'] = $new->renderHint(); |
659
|
|
|
} |
660
|
|
|
|
661
|
335 |
|
if (!array_key_exists('{label}', $new->parts)) { |
662
|
303 |
|
$new->parts['{label}'] = $new->renderLabel(); |
663
|
|
|
} |
664
|
|
|
|
665
|
335 |
|
return preg_replace('/^\h*\v+/m', '', trim(strtr($new->template, $new->parts))); |
666
|
|
|
} |
667
|
|
|
|
668
|
|
|
/** |
669
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
670
|
|
|
*/ |
671
|
334 |
|
private function renderHint(): string |
672
|
|
|
{ |
673
|
334 |
|
$hintAttributes = $this->getHintAttributes(); |
674
|
334 |
|
$hintClass = $this->getHintIndividualClass($this->type) ?? $this->getHintClass(); |
675
|
|
|
|
676
|
334 |
|
if ($hintClass !== '') { |
677
|
5 |
|
Html::addCssClass($hintAttributes, $hintClass); |
678
|
|
|
} |
679
|
|
|
|
680
|
334 |
|
if ($this->getAriaDescribedBy() === true) { |
681
|
|
|
$hintAttributes['id'] = $this->inputWidget->getInputId(); |
682
|
|
|
} |
683
|
|
|
|
684
|
334 |
|
return Hint::widget() |
685
|
334 |
|
->attributes($hintAttributes) |
686
|
334 |
|
->encode($this->getEncode()) |
687
|
334 |
|
->for($this->inputWidget->getFormModel(), $this->inputWidget->getAttribute()) |
688
|
334 |
|
->hint($this->getHint()) |
689
|
334 |
|
->tag($this->getHintTag()) |
690
|
334 |
|
->render(); |
691
|
|
|
} |
692
|
|
|
|
693
|
|
|
/** |
694
|
|
|
* @throws CircularReferenceException|InvalidConfigException|NotFoundException|NotInstantiableException |
695
|
|
|
*/ |
696
|
303 |
|
private function renderLabel(): string |
697
|
|
|
{ |
698
|
303 |
|
$labelAttributes = $this->getLabelAttributes(); |
699
|
303 |
|
$labelClass = $this->getLabelIndividualClass($this->type) ?? $this->getLabelClass(); |
700
|
|
|
|
701
|
303 |
|
if (!array_key_exists('for', $labelAttributes)) { |
702
|
|
|
/** @var string */ |
703
|
301 |
|
$labelAttributes['for'] = ArrayHelper::getValue($this->attributes, 'id', $this->inputWidget->getInputId()); |
704
|
|
|
} |
705
|
|
|
|
706
|
303 |
|
if ($labelClass !== '') { |
707
|
3 |
|
Html::addCssClass($labelAttributes, $labelClass); |
708
|
|
|
} |
709
|
|
|
|
710
|
303 |
|
return Label::widget() |
711
|
303 |
|
->attributes($labelAttributes) |
712
|
303 |
|
->encode($this->getEncode()) |
713
|
303 |
|
->for($this->inputWidget->getFormModel(), $this->inputWidget->getAttribute()) |
714
|
303 |
|
->label($this->getLabel()) |
715
|
303 |
|
->render(); |
716
|
|
|
} |
717
|
|
|
} |
718
|
|
|
|