outl1ne / nova-settings

A Laravel Nova tool for editing custom settings using native Nova fields.
MIT License
274 stars 96 forks source link

default method doesn't work #18

Closed phuclh closed 3 years ago

phuclh commented 4 years ago
Text::make('Uuid')->default(function ($request) {
    return 'abc';
})

Is there any ways to use the default method on this package? Now it just leaves a blank input. Thank you!

liorocks commented 4 years ago

@phuclh No, but this is how you can achieve at this moment:

Text::make('Some Setting', 'key')->withMeta([
    'value' => nova_get_setting('key', 'Default Value')
])
Tarpsvo commented 4 years ago

There's one issue with that. What if there's a field that has a default value but the user wants it to be null (or empty). They would have to empty the field every time, since it would get prefilled with the default value.

Tarpsvo commented 4 years ago

Actually, nevermind, the ->default() would never work properly, I don't think. As the serialization function in Nova does the following:

'value' => $this->resolveDefaultValue($request) ?? $this->value,

If I emulated a 'create' request so resolveDefaultValue would actually resolve the default value, it would overwrite the actual value each time.

However, what I could do is create a 'defaultSetting' macro that does the same thing as @Landish's code. So you could call ->defaultSetting('bleh') on any field. Would that be close enough?