fix: 🐛 cast boolean dynamic fields recursively
This commit is contained in:
parent
a4ab004959
commit
32ce7bb8e1
1 changed files with 27 additions and 7 deletions
|
|
@ -92,13 +92,9 @@ abstract class UpdateDynamicFieldRequest extends FormRequest
|
|||
]);
|
||||
break;
|
||||
case DynamicFieldTypes::boolean->value:
|
||||
$all = $this->all();
|
||||
$nested = ToolboxHelper::dotStringToNestedArray(
|
||||
$ruleKey,
|
||||
$this->input($ruleKey) ? true : false
|
||||
);
|
||||
$all = ToolboxHelper::arrayMergeRecursiveDistinct($all, $nested);
|
||||
$this->merge($all);
|
||||
$all = $this->all();
|
||||
$modified = $this->castBooleanFields($all);
|
||||
$this->merge($modified);
|
||||
$rules = array_merge($rules, [
|
||||
$ruleKey => $this->compileValidate(DynamicFieldTypes::boolean->getValidation(), $input)
|
||||
]);
|
||||
|
|
@ -279,4 +275,28 @@ abstract class UpdateDynamicFieldRequest extends FormRequest
|
|||
}
|
||||
return $itemKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively traverses an array and converts it to Boolean
|
||||
* all values whose key begins with “boolean_”.
|
||||
*
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
private function castBooleanFields(array $data): array
|
||||
{
|
||||
foreach ($data as $key => $value) {
|
||||
// If the value is an array, recursive method.
|
||||
if (is_array($value)) {
|
||||
$data[$key] = $this->castBooleanFields($value);
|
||||
continue;
|
||||
}
|
||||
// Check if the key start with the pattern enum_.
|
||||
if (str_starts_with($key, sprintf("%s_", DynamicFieldTypes::boolean->name))) {
|
||||
// Manages values "true", "false", "1", "0", 1, 0, "on", "off".
|
||||
$data[$key] = filter_var($value, FILTER_VALIDATE_BOOLEAN);
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue