Skip to content

Commit e57544c

Browse files
authored
Merge pull request #5279 from Laravel-Backpack/throw-error-when-missing-subfields
Improve subfields errors on handling
2 parents c39ccb8 + 1a4f041 commit e57544c

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/app/Library/CrudPanel/Traits/FieldsProtectedMethods.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,17 @@ protected function inferFieldTypeFromRelationType($relationType)
254254
*/
255255
protected function makeSureSubfieldsHaveNecessaryAttributes($field)
256256
{
257-
if (! isset($field['subfields'])) {
257+
if (! isset($field['subfields']) || ! is_array($field['subfields'])) {
258258
return $field;
259259
}
260260

261+
if (! is_multidimensional_array($field['subfields'], true)) {
262+
abort(500, 'Subfields of «'.$field['name'].'» are malformed. Make sure you provide an array of subfields.');
263+
}
264+
261265
foreach ($field['subfields'] as $key => $subfield) {
262266
if (empty($subfield) || ! isset($subfield['name'])) {
263-
abort(500, 'Subfield name can\'t be empty');
267+
abort(500, 'A subfield of «'.$field['name'].'» is malformed. Subfield attribute name can\'t be empty.');
264268
}
265269

266270
// make sure the field definition is an array

src/helpers.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,20 +365,22 @@ function old_empty_or_null($key, $empty_value = '')
365365

366366
if (! function_exists('is_multidimensional_array')) {
367367
/**
368-
* If any of the items inside a given array is an array, the array is considered multidimensional.
368+
* Check if the array is multidimensional.
369369
*
370-
* @param array $array
371-
* @return bool
370+
* If $strict is enabled, the array is considered multidimensional only if all elements of the array are arrays.
372371
*/
373-
function is_multidimensional_array(array $array)
372+
function is_multidimensional_array(array $array, bool $strict = false): bool
374373
{
375374
foreach ($array as $item) {
376-
if (is_array($item)) {
375+
if ($strict && ! is_array($item)) {
376+
return false;
377+
}
378+
if (! $strict && is_array($item)) {
377379
return true;
378380
}
379381
}
380382

381-
return false;
383+
return $strict;
382384
}
383385
}
384386

0 commit comments

Comments
 (0)