-
-
Notifications
You must be signed in to change notification settings - Fork 256
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?
Add support for custom validation messages and attributes when using property morphable #1055
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 |
|
Thanks for the PR, a few changes are required. @bentleyo Do we need changes to support the cases @ncphillips is mentioning? By the way thanks for the postcard! |
…perty-morphable-custom-messages # Conflicts: # src/Resolvers/DataValidationMessagesAndAttributesResolver.php
@ncphillips sorry I took so long to reply! Thanks for this.
Update: I misunderstood, I'm able to replicate now! Will look into it. |
@rubenvanassche sorry for the delay! I'm glad you got the postcard 🎉 I checked the Spatie postcard wall website a few times after the initial failed send and I thought it must be ended up in the wrong place again 😂 I hope the postcard inside-an-envelope, inside-another-envelope gave you a laugh. It's really not the way a postcard is meant to work is it?! I've completed the changes you requested. |
|
@rubenvanassche I've updated the code to handle the case @ncphillips mentioned (property-morphable data inside an array of static data). The problem was that we were trying to determine the property-morphable data class from a path like To tackle this problem I implemented an approach that converts wildcard paths to static paths when a property-morphable data class is encountered (e.g. I implemented it in a way that should work with deeply nested wildcard path conversion. Let me know if you have any questions or think of any potential problems! |
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
DataValidationMessagesAndAttributesResolverclass aware of property-morphable data.There is a section of code I added that is now repeated in both:
DataValidationMessagesAndAttributesResolverDataValidationRulesResolverNot sure if it should be moved somewhere shared to avoid this duplication.
The code in question: