Set default value for colorpicker.
I'm trying to set some default values for my colorpicker but the all fall back to 'black'. Probably because the value is empty. My code looks like this:
Any ideas?
4 Replies
on a resource? create or edit page?
Hi Dan, only happens when I edit the resource. Creating an item seems to be respecting the default values..
You can't have a default on an existing item. You'll need to format state using if that's the case for exisitng items.
default's only apply to new.
Yeah, if you need a "default" on existing one, just work with afterStateHydrated, and check if its already set, otherwise set a default. Like this:
->afterStateHydrated(function (Set $set, YourModel $yourModel) { $set('your_model_attr', $yourModel->your_model_attr ?? "default"); })