@@ -23,8 +23,14 @@ class FormFieldPrefixer
23
23
protected $ arrayKey = null ;
24
24
25
25
/**
26
- * Indicator if we are generating a form field name
27
- * with a multi dimensional array.
26
+ * Indicator if we are generating a form field name as an array.
27
+ *
28
+ * @var bool
29
+ */
30
+ protected $ isArray = false ;
31
+
32
+ /**
33
+ * Indicator if we are generating a form field name with a multi dimensional array.
28
34
*
29
35
* @var bool
30
36
*/
@@ -75,9 +81,10 @@ public function withPrefix($prefix)
75
81
*
76
82
* @return $this
77
83
*/
78
- public function asArray ($ arrayKey , $ multiDimensional = false )
84
+ public function asArray ($ arrayKey = null , $ multiDimensional = false )
79
85
{
80
86
$ this ->arrayKey = $ arrayKey ;
87
+ $ this ->isArray = true ;
81
88
$ this ->multiDimensional = $ multiDimensional && $ this ->prefix !== null ;
82
89
83
90
return $ this ;
@@ -229,11 +236,11 @@ public function validationKey($key)
229
236
*/
230
237
public function isArray ()
231
238
{
232
- return ! is_null ( $ this ->arrayKey ) ;
239
+ return $ this ->isArray ;
233
240
}
234
241
235
242
/**
236
- * Check if the form field is an array.
243
+ * Check if the form field is a multi dimensional array.
237
244
*
238
245
* @return bool
239
246
*/
@@ -252,6 +259,16 @@ public function hasPrefix()
252
259
return !! $ this ->prefix ;
253
260
}
254
261
262
+ /**
263
+ * Check if the form field has an array key.
264
+ *
265
+ * @return bool
266
+ */
267
+ public function hasArrayKey ()
268
+ {
269
+ return ! is_null ($ this ->arrayKey );
270
+ }
271
+
255
272
/**
256
273
* Build the attribute.
257
274
*
@@ -307,7 +324,7 @@ protected function buildAttributeValue($name, $useArraySyntax, $separator = null
307
324
$ separator = $ separator ?: $ this ->getDefaultSeparator ();
308
325
309
326
$ prefix = $ this ->buildName ($ name );
310
- $ arrayKey = $ this ->buildArrayKey ($ useArraySyntax , $ separator );
327
+ $ arrayKey = $ this ->buildArrayKey ($ name , $ useArraySyntax , $ separator );
311
328
$ arrayName = $ this ->buildArrayName ($ name , $ useArraySyntax , $ separator );
312
329
313
330
$ identifier = $ prefix . $ arrayKey . $ arrayName ;
@@ -332,7 +349,7 @@ protected function buildName($name)
332
349
return $ name ;
333
350
}
334
351
335
- if ($ this ->isMultiDimensionalArray ()) {
352
+ if ($ this ->isMultiDimensionalArray () || ( $ this -> isArray () && ! $ this -> hasArrayKey ()) ) {
336
353
return $ this ->prefix ;
337
354
}
338
355
@@ -342,18 +359,19 @@ protected function buildName($name)
342
359
/**
343
360
* Build the array key part of the form field identifier if needed.
344
361
*
362
+ * @param string $name
345
363
* @param bool $useArraySyntax
346
364
* @param string $separator
347
365
*
348
366
* @return string
349
367
*/
350
- protected function buildArrayKey ($ useArraySyntax , $ separator )
368
+ protected function buildArrayKey ($ name , $ useArraySyntax , $ separator )
351
369
{
352
370
if ( ! $ this ->isArray ()) {
353
371
return '' ;
354
372
}
355
373
356
- return $ this ->buildArrayIdentifier ($ this ->arrayKey , $ useArraySyntax , $ separator );
374
+ return $ this ->buildArrayIdentifier ($ this ->arrayKey ?: $ name , $ useArraySyntax , $ separator );
357
375
}
358
376
359
377
/**
@@ -368,7 +386,7 @@ protected function buildArrayKey($useArraySyntax, $separator)
368
386
*/
369
387
protected function buildArrayName ($ name , $ useArraySyntax , $ separator )
370
388
{
371
- if ( ! $ this ->hasPrefix () || ! $ this -> isMultiDimensionalArray ()) {
389
+ if ( ! $ this ->isMultiDimensionalArray ()) {
372
390
return '' ;
373
391
}
374
392
@@ -398,7 +416,7 @@ protected function buildArrayIdentifier($value, $useArraySyntax, $separator)
398
416
*/
399
417
protected function buildJavaScriptValueKey ($ key )
400
418
{
401
- if ($ this ->isMultiDimensionalArray ()) {
419
+ if ($ this ->isMultiDimensionalArray () || ( $ this -> isArray () && ! $ this -> hasArrayKey ()) ) {
402
420
$ key = "' {$ key }' " ;
403
421
}
404
422
0 commit comments