F
Filament2w ago
borg

OptionStateCast forces casting of option keys to integer

@Dan Harrin Any particular reason why would you force the type to integer? This last update does solve for keys starting with zero, but to be honest I currently don't agree. In my particular case, I have keys in this fashion "0011, 1025, 5568" which I'd like to keep as strings no matter what. Changing the type throughout the application can have side effects. My DB columns is JSON and the values get's stored as string in case of leading zeroes and integer in other cases. I solved this with setting my custom cast with ->stateCasts(), now after this update: https://github.com/filamentphp/filament/pull/17902/files I solved with Cutom Laravel casts, since you keep numbers starting with zero as strings. Not everyone knows this if they don't source dive and will have issues. TLDR: Why do you force integer if ctype_digit return true? Maybe have an option that keeps string eg ->enforceStringState(). I will consider a PR for this, but I'd just like to know the reasoning behind this. I may be wrong with my input. Thank you!
3 Replies
Dan Harrin
Dan Harrin2w ago
Generally, when people put an int into the Select, they expect an int back. We need them to be strings internally so that the component works correctly, but once they are retrieved again they need to be converted. The 99% use case for a Select is for putting an autoincrementing ID in and getting one back, and its very rare for them to start with 0 People doing === strict comparison checks between state and model IDs will have their code fail if they are not integers
borg
borgOP2w ago
I can understand that, but: The docs primarily provide "string" examples throughout the Select documentation. This is misleading if you defend the "99% usecases are integers". However, don't get me wrong, I respect filament and everything you do. I'm just here to discuss this without taking too much of your time. Maybe the docs are the ones missing this remark. Thanks for taking your time to reply @Dan Harrin . Cheers
Dan Harrin
Dan Harrin2w ago
Sorry, I should have phrased that "99% of numeric use cases are an autoincrementing ID" The string examples work best for explaining how to manually pass options in But I think enums and relationships (https://filamentphp.com/docs/4.x/forms/select#integrating-with-an-eloquent-relationship) are most used, and relationships are usually integers

Did you find this page helpful?