3
3
namespace Kirby \Form ;
4
4
5
5
use Closure ;
6
- use Kirby \Cms \HasSiblings ;
7
6
use Kirby \Exception \InvalidArgumentException ;
8
7
use Kirby \Toolkit \A ;
9
8
use Kirby \Toolkit \Component ;
10
- use Kirby \Toolkit \I18n ;
11
- use Kirby \Toolkit \Str ;
12
9
13
10
/**
14
11
* Form Field object that takes a Vue component style
23
20
*/
24
21
class Field extends Component
25
22
{
26
- /**
27
- * @use \Kirby\Cms\HasSiblings<\Kirby\Form\Fields>
28
- */
29
- use HasSiblings;
30
- use Mixin \Api;
23
+ use Mixin \Common;
24
+ use Mixin \Endpoints;
31
25
use Mixin \Model;
26
+ use Mixin \Siblings;
32
27
use Mixin \Translatable;
33
28
use Mixin \Validation;
34
29
use Mixin \When;
35
30
use Mixin \Value;
36
31
37
- /**
38
- * Parent collection with all fields of the current form
39
- */
40
- public Fields $ siblings ;
41
-
42
32
/**
43
33
* Registry for all component mixins
44
34
*/
@@ -81,8 +71,22 @@ public function __construct(
81
71
82
72
parent ::__construct ($ type , $ attrs );
83
73
84
- // set the siblings collection
85
- $ this ->siblings = $ siblings ?? new Fields ([$ this ]);
74
+ $ this ->setSiblings ($ attrs ['siblings ' ] ?? null );
75
+ }
76
+
77
+ /**
78
+ * Returns field api routes
79
+ */
80
+ public function api (): array
81
+ {
82
+ if (
83
+ isset ($ this ->options ['api ' ]) === true &&
84
+ $ this ->options ['api ' ] instanceof Closure
85
+ ) {
86
+ return $ this ->options ['api ' ]->call ($ this );
87
+ }
88
+
89
+ return [];
86
90
}
87
91
88
92
/**
@@ -95,38 +99,38 @@ public static function defaults(): array
95
99
/**
96
100
* Optional text that will be shown after the input
97
101
*/
98
- 'after ' => function ($ after = null ) {
99
- return I18n:: translate ( $ after , $ after );
102
+ 'after ' => function (array | string | null $ after = null ) {
103
+ return $ this -> i18n ( $ after );
100
104
},
101
105
/**
102
106
* Sets the focus on this field when the form loads. Only the first field with this label gets
103
107
*/
104
- 'autofocus ' => function (bool | null $ autofocus = null ): bool {
105
- return $ autofocus ?? false ;
108
+ 'autofocus ' => function (bool $ autofocus = false ): bool {
109
+ return $ autofocus ;
106
110
},
107
111
/**
108
112
* Optional text that will be shown before the input
109
113
*/
110
- 'before ' => function ($ before = null ) {
111
- return I18n:: translate ( $ before , $ before );
114
+ 'before ' => function (array | string | null $ before = null ) {
115
+ return $ this -> i18n ( $ before );
112
116
},
113
117
/**
114
118
* Default value for the field, which will be used when a page/file/user is created
115
119
*/
116
- 'default ' => function ($ default = null ) {
120
+ 'default ' => function (mixed $ default = null ) {
117
121
return $ default ;
118
122
},
119
123
/**
120
124
* If `true`, the field is no longer editable and will not be saved
121
125
*/
122
- 'disabled ' => function (bool | null $ disabled = null ): bool {
123
- return $ disabled ?? false ;
126
+ 'disabled ' => function (bool $ disabled = false ): bool {
127
+ return $ disabled ;
124
128
},
125
129
/**
126
130
* Optional help text below the field
127
131
*/
128
- 'help ' => function ($ help = null ) {
129
- return I18n:: translate ( $ help , $ help );
132
+ 'help ' => function (array | string | null $ help = null ) {
133
+ return $ this -> i18n ( $ help );
130
134
},
131
135
/**
132
136
* Optional icon that will be shown at the end of the field
@@ -137,20 +141,20 @@ public static function defaults(): array
137
141
/**
138
142
* The field label can be set as string or associative array with translations
139
143
*/
140
- 'label ' => function ($ label = null ) {
141
- return I18n:: translate ( $ label , $ label );
144
+ 'label ' => function (array | string | null $ label = null ) {
145
+ return $ this -> i18n ( $ label );
142
146
},
143
147
/**
144
148
* Optional placeholder value that will be shown when the field is empty
145
149
*/
146
- 'placeholder ' => function ($ placeholder = null ) {
147
- return I18n:: translate ( $ placeholder , $ placeholder );
150
+ 'placeholder ' => function (array | string | null $ placeholder = null ) {
151
+ return $ this -> i18n ( $ placeholder );
148
152
},
149
153
/**
150
154
* If `true`, the field has to be filled in correctly to be saved.
151
155
*/
152
- 'required ' => function (bool | null $ required = null ): bool {
153
- return $ required ?? false ;
156
+ 'required ' => function (bool $ required = false ): bool {
157
+ return $ required ;
154
158
},
155
159
/**
156
160
* If `false`, the field will be disabled in non-default languages and cannot be translated. This is only relevant in multi-language setups.
@@ -161,62 +165,18 @@ public static function defaults(): array
161
165
/**
162
166
* Conditions when the field will be shown (since 3.1.0)
163
167
*/
164
- 'when ' => function ($ when = null ) {
168
+ 'when ' => function (array | null $ when = null ) {
165
169
return $ when ;
166
170
},
167
171
/**
168
172
* The width of the field in the field grid. Available widths: `1/1`, `1/2`, `1/3`, `1/4`, `2/3`, `3/4`
169
173
*/
170
- 'width ' => function (string $ width = ' 1/1 ' ) {
174
+ 'width ' => function (string | null $ width = null ) {
171
175
return $ width ;
172
176
},
173
177
'value ' => function ($ value = null ) {
174
178
return $ value ;
175
179
}
176
- ],
177
- 'computed ' => [
178
- 'after ' => function () {
179
- /** @var \Kirby\Form\Field $this */
180
- if ($ this ->after !== null ) {
181
- return $ this ->model ()->toString ($ this ->after );
182
- }
183
- },
184
- 'before ' => function () {
185
- /** @var \Kirby\Form\Field $this */
186
- if ($ this ->before !== null ) {
187
- return $ this ->model ()->toString ($ this ->before );
188
- }
189
- },
190
- 'default ' => function () {
191
- /** @var \Kirby\Form\Field $this */
192
- if ($ this ->default === null ) {
193
- return ;
194
- }
195
-
196
- if (is_string ($ this ->default ) === false ) {
197
- return $ this ->default ;
198
- }
199
-
200
- return $ this ->model ()->toString ($ this ->default );
201
- },
202
- 'help ' => function () {
203
- /** @var \Kirby\Form\Field $this */
204
- if ($ this ->help ) {
205
- $ help = $ this ->model ()->toSafeString ($ this ->help );
206
- $ help = $ this ->kirby ()->kirbytext ($ help );
207
- return $ help ;
208
- }
209
- },
210
- 'label ' => function () {
211
- /** @var \Kirby\Form\Field $this */
212
- return $ this ->model ()->toString ($ this ->label ?? Str::ucfirst ($ this ->name ));
213
- },
214
- 'placeholder ' => function () {
215
- /** @var \Kirby\Form\Field $this */
216
- if ($ this ->placeholder !== null ) {
217
- return $ this ->model ()->toString ($ this ->placeholder );
218
- }
219
- }
220
180
]
221
181
];
222
182
}
@@ -281,30 +241,14 @@ public function fill(mixed $value): static
281
241
$ this ->applyProp ('value ' , $ this ->options ['props ' ]['value ' ] ?? $ value );
282
242
283
243
// reevaluate the computed props
284
- $ this ->applyComputed ($ this ->options ['computed ' ]);
244
+ $ this ->applyComputed ($ this ->options ['computed ' ] ?? [] );
285
245
286
246
// reset the errors cache
287
247
$ this ->errors = null ;
288
248
289
249
return $ this ;
290
250
}
291
251
292
- /**
293
- * @deprecated 5.0.0 Use `::siblings() instead
294
- */
295
- public function formFields (): Fields
296
- {
297
- return $ this ->siblings ;
298
- }
299
-
300
- /**
301
- * Checks if the field is disabled
302
- */
303
- public function isDisabled (): bool
304
- {
305
- return $ this ->disabled === true ;
306
- }
307
-
308
252
/**
309
253
* Checks if the field is hidden
310
254
*/
@@ -313,14 +257,6 @@ public function isHidden(): bool
313
257
return ($ this ->options ['hidden ' ] ?? false ) === true ;
314
258
}
315
259
316
- /**
317
- * Checks if the field is required
318
- */
319
- public function isRequired (): bool
320
- {
321
- return $ this ->required ?? false ;
322
- }
323
-
324
260
/**
325
261
* Checks if the field is saveable
326
262
*/
@@ -329,46 +265,6 @@ public function isSaveable(): bool
329
265
return ($ this ->options ['save ' ] ?? true ) !== false ;
330
266
}
331
267
332
- /**
333
- * Returns field api routes
334
- */
335
- public function routes (): array
336
- {
337
- if (
338
- isset ($ this ->options ['api ' ]) === true &&
339
- $ this ->options ['api ' ] instanceof Closure
340
- ) {
341
- return $ this ->options ['api ' ]->call ($ this );
342
- }
343
-
344
- return [];
345
- }
346
-
347
- /**
348
- * Checks if the field is saveable
349
- * @deprecated 5.0.0 Use `::isSaveable()` instead
350
- */
351
- public function save (): bool
352
- {
353
- return $ this ->isSaveable ();
354
- }
355
-
356
- /**
357
- * Parent collection with all fields of the current form
358
- */
359
- public function siblings (): Fields
360
- {
361
- return $ this ->siblings ;
362
- }
363
-
364
- /**
365
- * Returns all sibling fields for the HasSiblings trait
366
- */
367
- protected function siblingsCollection (): Fields
368
- {
369
- return $ this ->siblings ;
370
- }
371
-
372
268
/**
373
269
* Converts the field to a plain array
374
270
*/
0 commit comments