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:
ColorPicker::make('color_2')->label('Kleur 2')->default('#00AB9A')->reactive()
ColorPicker::make('color_2')->label('Kleur 2')->default('#00AB9A')->reactive()
Any ideas?
4 Replies
Dan Harrin
Dan Harrin2y ago
on a resource? create or edit page?
jaap4677
jaap4677OP17mo ago
Hi Dan, only happens when I edit the resource. Creating an item seems to be respecting the default values..
toeknee
toeknee17mo ago
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.
Atreides
Atreides4mo ago
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"); })
Want results from more Discord servers?
Add your server