-
Notifications
You must be signed in to change notification settings - Fork 404
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
Reuse of validations from different models #519
Comments
I'd like to see the code because for local validators it should work as you expect. For remote validators like uniqueness it won't. |
That's correct, it does work for local validators, I'm proposing to make it work for remote |
To make it work with remote you should be able to override the <% form_for @user_registration_form, :as => :user %>
.. |
hm, just tried it out and it didn't work, i'll play with it a bit more and will report back |
Can you show me what the resulting HTML code is? The remote validator middleware parses the model name based upon the |
Ok, looks like I found a bug: I start with this code in the template <% form_for(resource, as: :user %>
... JS that is produced is this: "validators":
{"user[email]":{
"presence":[{"message":"Required"}],
"uniqueness":[{"message":"Already in use.","case_sensitive":true,"class":"accounts/user_registration_form"}]}
}}; When I update the template to this <% form_for(resource, as: :new_model %>
... Validation JS is this: "validators":
{"new_model[email]":{
"presence":[{"message":"Required"}],
"uniqueness":[{"message":"Already in use.","case_sensitive":true,"class":"accounts/user_registration_form"}]}
}}; So the "class" key in the remote validation is still the same although other references are updated |
Ah, yeah that does look like a bug. Ok, I'll keep this open and try to get to it soon. I'm pretty swamped right now so maybe not immediately :( |
Cool I'll try to mock around as well sent from aPhone
|
It behaves that way because we set validation options in the ActiveModel (or, in case of uniqueness validator, in ActiveRecord) and don't mess with it in ActionView, where we set I think passing real model name in validator options is ok since, well, that's a wrapper object and we have already jumped through a couple of hoops making it quack like original one :) Plus, making If my speculation makes sense, I'll hack it up in an hour (because testing it looks pretty painful) |
Oh, and it fails only when wrapper class is namespaced (note |
thanks for the fast response @arr-ee! I was under impression that that @bcardarella didn't want to add any extra options. |
@ilyakatz, yeah, I thought that too, but after a bit of playing with code I thought it would be better solution. Adding workaround for this somewhere in ActiveView seemed even more messed up. |
@ilyakatz, by the way, could you please test if pull request actually resolves the problem? |
sure, will try it out. kind of agree with the implications on ActiveView as well |
@arr-ee just tried it out, the request works fine. one problem is that if I don't specify a message, I get an error
which makes sense since it is not an AR model so it doesn't fall back to activerecord.errors localization/defaults. Not sure if it's a good idea to add fallback to the gem or just document that message is required |
That's not a gem's problem, actually, your wrapper just doesn't quack hard enough. Try adding this to you wrapper class: extend ActiveModel::Naming
extend ActiveModel::Translation
def self.model_name
@_model_name ||= ActiveModel::Name.new(User) # or whatever your actual class is
end
def self.i18n_scope
:activerecord
end |
ok cool, in that case, i'm happy - you made me happy :) I actually don't want to add all that stuff in, so I will play around it |
Great! Anyway, those modules are strongly recommended because they make your form objects behave more like actual models (actually, in Rails 4 they are conveniently packed into ActiveModel::Model). I'll wait for @bcardarella comments on this and if he's good with it, I'll merge it in upstream. |
@arr-ee this is OK as it doesn't really violate semver. I look at this as fixing a bug. I will comment inline on some changes I would make before merging. |
Fixed in #520. |
Thanks for the fast turnaround time, @arr-ee and @bcardarella |
I find it that people often want to create a separate object that incapsulates model that the form is being created for, something along the lines of http://pivotallabs.com/form-backing-objects-for-fun-and-profit/
Yesterday I found myself in the situation where I wanted to use CSV for one such object and it worked with basic client side validation like presence
However, I wanted to go further to reuse ajax-based validation
That didn't work because it was trying to validate against UserRegistrationForm in CSV which makes sense.
I'm proposing to allow a syntax like this
I'd be happy to give it a shot to code it up but wanted to run it past you first.
The text was updated successfully, but these errors were encountered: