Nullable PHP Enum in Table TextColumn Oddity
I have a model/table that is a collection of various statuses (PHP Enum values) that are nullable if it's never been set.
When using a TextColumn like so:
If the field actually has an enum value, it works and shows the badge as expected. If the value in the column is null, then the placeholder (nor
default
) doesn't properly set a 'fallback' state.
What am I missing?21 Replies
Maybe getStateUsing to return a default if null?
Hmm that does set it. Would that break the 'magical' HasLabel / HasColor interfaces for enums?
I guess I should make an 'unknown' state?
Hmm surprised to see that 'null' values don't trigger the default / placeholder 'handlers'
I guess that's due to the Enum cast on the model's attribute?
Could be
Not sure
Ok, well still appreciate the help!
I suppose I can make an 'unknown' enum state
but not ever 'store' that in the db
so the null db state would trigger the frontend's 'unknown' handler state. Hmm guess that means I'd have to do that 'handling' everywhere
Been thinking about this and I haven't looked at the relevant filament source code yet but I think I understand why this is 'diifficult to handle' in terms of how could you '->getLabel/Color' on a null enum
But perhaps I could do a PR that would add
->defaultLabel()
and ->defaultColor
for the TextColumn when it's handling an enum that is null
Or maybe a defaultLabel / defaultColor interface you could add to your enum that has static function signatures for getDefaultLabel and getDefaultColor respectively
Not sure which would be betterThere is a cool trick you can do. You can cast the column to an Enum via the model
I do that
And I think everything else is handled automatically by filament
That's how it takes advantage of the getLabel and getColor
The problem is that it's a nullable field
like if we have never received a status update for that field from the external system, the column/attribute/enum value is null.
So it leaves the column / badge blank
I was expecting the placeholder / default method on the textcolumn to allow me to set what to show when the attribute is null
Isnt there a defaut case in the Enum?
I think so
🙂 that was my first thought
but that is nonsensical
since you can't call ->getLabel on a null value
But you call getLabel based on the case, no?
if you have a case
Not sure if i understand what you want :/
Ah I hadn't considered actually having a case where the value is null
Try this, and let me know
But I think that would cause a PHP error since the backing type of the enum is "string" instead of "?string" or "string | null"
I believe the only valid backing types are int and string
yes, pls change that
Yeah null is not considered a valid scalar
Ok I have another idea
Another method is to create a specific case to represent the "null" or "undefined" state, and use a different backing value (like an empty string or a specific keyword) for it.
That was one option I had considered but due to some reasons on the backend, I probably need to stick with the nullable value
Can you try this?
Wonder if you could just use default on the match statements. Still thinks it’s a caveat in the table though where null values just aren’t rendered.
I've seen a few other reports of this same issue, here in Discord. Seems reasonable to me to expect that a null should allow
placeholder()
and default()
to operate. Maybe we just need the component to check for being cast to enum? or maybe the current checking is doing a specific override when the enum is null, and we need to stop that override?