-
-
Notifications
You must be signed in to change notification settings - Fork 249
Add support for custom validation messages and attributes when using property morphable #1055
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…property morphable
We're also running into this issue so I'm very excited for this fix. We make use of polymorphic collections in a couple places and ranging their error messages has been really frustrating. Edit: If anyone else needs this, you can copy bentley's class into your own app and bind it to the container: |
Actually, this doesn't seem to be a complete fix @bentleyo It works if I do Will investigate more later |
Okay, I got a test that reproduced the issue I was seeing, and updated the code to make that test pass. Opened a PR into this branch @bentleyo |
Thanks @ncphillips I'll have a look this evening! |
We've found a couple other problematic cases. Case 1: Nullable Polymorphic ChildThe first is when an object has a nullable polymoprhic child and it happens to be null: class Vehicle {}
class Car extends Vehicle {}
class Truck extends Vehicle {}
class Garage {
vehicle?: Vehicle
} Note: I opened a PR into this branch to try and fix it, but I've just realized it's failing. bentleyo#3 Case 2: Polymorphic Children of Non-Polymorpch Items in a CollectionThe second case is more annoying. We changed our data structure to avoid the problem, but it's still worth mentioning. If you have an array of non-polymorphic objects, but those objects themselves have a polymorphic child, then laravel-data will not check any further down. For example, if I had a Region that contained various garages: class Region {
garages: Garage[]
} This would fail to use the custom validation messages/attributes for the Vehicles in the Garage |
Custom validation messages and attribute names don't work properly when using property-morphable data at the moment. They build messages from the base class, not the morphed one, even when the morph attributes are provided.
This PR fixes that by making the
DataValidationMessagesAndAttributesResolver
class aware of property-morphable data.There is a section of code I added that is now repeated in both:
DataValidationMessagesAndAttributesResolver
DataValidationRulesResolver
Not sure if it should be moved somewhere shared to avoid this duplication.
The code in question: