Default value for Settings page form
I'm using the Spatie Settings plugin but some input need to be able to be empty, the problem is that the page will display error if some input have no data, is there an easy way to save value to empty string if user input no data, other than using lifecycle hook (i'm using mutateFormDataBeforeSave, but it's a pain if the page have many form) ?
Solution:Jump to solution
in the end i'm using array_map in mutateFormDataBeforeSave
protected function mutateFormDataBeforeSave(array $data): array
{
//? Loop through data and check if there's null value then replace it with empty string....
3 Replies
Have you tried adding
->nullable()
to the form's input component?According to the documentation
"The field value can be empty. This rule is applied by default if the required rule is not present"
so i guess that's not it.
Solution
in the end i'm using array_map in mutateFormDataBeforeSave
protected function mutateFormDataBeforeSave(array $data): array
{
//? Loop through data and check if there's null value then replace it with empty string.
$data = array_map(function ($value) {
return $value === null ? "" : $value;
}, $data);
return $data;
}