Enum select with live() on Create and Edit form
Hi All,
I have a question, i use a select with Enum and some dependecie fields/fieldset on this value.
The fieldset
On create record this works, on edit record this works not.
That is because on create
$get('row_type')
is Enum and on Edit it is integer even tho i cast it in the model
For now i have create a helper to get this to work.
This is how i use it now
It works, but i think there is a better and simpler solution only dont know how.
Can anyone point me in the right direction?
Thanks4 Replies
up
Same problem here ! Up 🤚
The issue you’re facing is that when editing a record, the value retrieved from the $get('row_type') is an integer, whereas during the creation process, it's an Enum instance. Instead of modifying the visible condition, you can modify the field setup to always return an Enum instance, whether you're creating or editing. This can be done by transforming the value using the afterStateHydrated method.
Great solution @sadiqgoni13 ! On my side, I only use string back enums, so I made something similar but using a
FilamentService
with isEqualEnum()
method :
And then in my form I inject the service with the boot()
method :
And use it like that :
In this example, the ethnicity_details
field is visible only if ethnicity
value is EthnicityEnum::OTHER
.
If you want to support ->multiple()
selects, you could even do it using array_intersect()
: