-
Notifications
You must be signed in to change notification settings - Fork 49
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
Nested form objects during validation have the wrong ActiveModel::Name #36
Comments
@mcmire this isn't isolated to nested forms, as per the docs this is exactly how reform is designed to work. If you don't think you should have to do that, what do you propose instead? We don't get involved in trying to infer classes from names like rails does as it gets messy and brittle pretty quickly. |
@fran-worley I believe the issue I am describing is isolated to nested forms: it seems like there is custom behavior around nested forms such that they get a specific name that is different from the model name that a non-nested form would usually get. What I'm saying is that if you have a nested form such as a collection and it is called But say the default model name were So, what I'm proposing is to change the default model name for a nested form so that the name of the form class is used to generate that model name instead of the name of the collection/property. What do you think? |
@mcmire In the nested form, you need to use collection :items do
model LineItem |
@apotonick Right. I am using a separate form class, though -- I am saying class OrderItemForm < Reform::Form
model :order_item
end First, the form class is called OrderItemForm. Second (because inferring names from class names is nasty, I do agree), I am passing an OrderItem instance to the OrderItemForm instance. So it seems like between these two options, the form should already know what the model class is, without my having to specify it. |
Why is the naming so important in the first place? For nested forms, it doesn't call URL helpers or anything, and Reform's deserializer will map it back properly. |
@apotonick, it's important in rails because it influences the input names which will mess up your params hash.
…Sent from my iPhone
On 24 Feb 2017, at 09:12, Nick Sutterer <[email protected]<mailto:[email protected]>> wrote:
Why is the naming so important in the first place? For nested forms, it doesn't call URL helpers or anything, and Reform's deserializer will map it back properly.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#36 (comment)>, or mute the thread<https://github.com/notifications/unsubscribe-auth/ACNaJTsbYKFvVjRLfbEH62-hp_qryL-Eks5rfp8MgaJpZM4Kb3_4>.
|
The solution is to release Formular and leave this crap behind us, ahead into a brighter future. |
Formular looks like a good idea, but does it have anything to do with model validation error messages? Because that's what this issue is really about. Let me see if I can explain this again :) Reform::Form is essentially an ActiveModel::Model (or brings in ActiveModel functionality at least). When ActiveModel produces validation error messages, it will pull them from select keys in i18n. The namespace of these i18n keys is based off the ActiveModel::Name of the class. Usually the Name corresponds to the name of the class. In the case of nested forms, reform-rails actually changes it to match the name of the property or collection that was used to add that form to the parent form. Hence, that namespace could potentially be unintuitive, as I described above in my examples. (Yes, the Name is also used to generate input names, but that's not what I'm talking about here.) Does this make sense? |
I just stumbled into this as well. And I'm pretty sure the problem is in Why the name is important? In my case it's used in the error messages, leading to different behavior when I run a single test file for the nested form vs. the whole test suite(when the parent form messes the name). |
I just got bitten by this as well. In my case, the model name is used to generate the form ID that is picked up by JavaScript code. Everything worked fine in my computer, but in production the JavaScript didn't run at all because the model name was different, thus the ID was different. It took me a while to figure out why this happened. Granted, there other ways to solve my issue, but I steel feel that the piece of code that @RKushnir mentioned looks dangerous and could use some rethinking. |
Say you have two forms, and one is nested in relation to the other, and you have validations on both forms:
Also (for the sake of completion) say you have a generic setup like this in your controller:
Now let's say that you've want to provide custom translations for these validations. You can do this easily for the parent form:
But if you want to provide translations for the child form...
...this won't work. It seems that if you simulate the code in Rails that translates validation messages, you get something interesting:
What this means is, I actually need to say this in my locale file:
Now I can fix this by adding
model :order_item
to OrderItemForm, but 1) I just found out about that and 2) I don't think I should have to do that.I've been hunting around in Reform and I stumbled upon these lines: https://github.com/trailblazer/reform-rails/blob/master/lib/reform/form/active_model.rb#L34
Could this have anything to do with it? If not, do you know what's happening here?
The text was updated successfully, but these errors were encountered: