nova-flexible-content icon indicating copy to clipboard operation
nova-flexible-content copied to clipboard

Default value

Open tongtastic opened this issue 5 years ago • 8 comments

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

tongtastic avatar Mar 13 '20 18:03 tongtastic

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

voidgraphics avatar Apr 03 '20 11:04 voidgraphics

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

royduin avatar Apr 23 '20 11:04 royduin

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.

royduin avatar Apr 29 '20 08:04 royduin

@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? :)

voidgraphics avatar Apr 29 '20 08:04 voidgraphics

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.

royduin avatar Apr 29 '20 11:04 royduin

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:

  • creating new record => today + 1 year
  • editing with empty value => today + 1 year
  • editing with stored date => value from database

skydiver avatar Jul 03 '20 17:07 skydiver

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

ArtDepartmentMJ avatar Mar 11 '24 09:03 ArtDepartmentMJ

I'm wondering if anyone could actually show default layouts on the model create page of nova. i don't want the user to add all the layouts one by one

nikoo-eshratabadi avatar Dec 19 '24 12:12 nikoo-eshratabadi