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?
knoxville48
knoxville48OP2y ago
Hi Dan, only happens when I edit the resource. Creating an item seems to be respecting the default values..
toeknee
toeknee2y 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
Atreides6mo 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"); })

Did you find this page helpful?