whitecube / nova-flexible-content

Flexible Content & Repeater Fields for Laravel Nova
MIT License
791 stars 234 forks source link

Default value #157

Open tongtastic opened 4 years ago

tongtastic commented 4 years ago

Hi,

Is there a quick way to set the default value of a field within a flexible layout if no value is currently set? I have tried doing this with ->withMeta(), and passing the standard nova key pairs, however this only works on a fresh page load with previously saved items, not on creation of a new layout item. Any ideas?

Thanks

voidgraphics commented 4 years ago

Hi there, sounds like a bug to me! Thanks for reporting.

royduin commented 4 years ago

Related: https://github.com/whitecube/nova-flexible-content/issues/125

royduin commented 4 years ago

Since Laravel Nova 3.4.0 there is a default method, see: https://github.com/laravel/nova/pull/866 and the docs: https://nova.laravel.com/docs/3.0/resources/fields.html#default-values. I didn't test it yet but this could be the solution for this issue.

voidgraphics commented 4 years ago

@royduin oh fantastic! That is much better than the withMeta way. @tongtastic can you give it a try and see if it solves your problem? :)

royduin commented 4 years ago

Just tested it myself, it works when creating a new resource but doesn't work when adding a new layout when editing a resource. Which is preferable but logical that it doesn't work from Nova itself. Which means editing a resource will fill empty fields.

skydiver commented 4 years ago

Wanna share my current implementation (it's a very particular case, but you will get the idea):

Date::make('Scheduled', 'scheduled_at', function ($value) {
    return $value ?? Carbon::now()->addYear();
})->rules('required'),

Will result in:

ArtDepartmentMJ commented 8 months ago

I'm experiencing this but only on existing models, when creating a new one the default values come through fine.