-
Notifications
You must be signed in to change notification settings - Fork 403
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
Multiple fields validations on rails5 #765
Comments
Hi,
About the wiki: https://github.com/DavyJonesLocker/client_side_validations/wiki/Shared-validations-for-multiple-fields Quoting from the wiki:
Well, the example is outdated and I'm not a fan of the approach suggested by that wiki. If you just need that kind of validation, the numericality validator offers that kind of validation out of the box: https://guides.rubyonrails.org/active_record_validations.html#numericality validates :small_field, numericality: { greater_than: 0 }
validates :great_field, numericality: { greater_than: :small_field } It works on the client side in a lot of cases (other cases will be anyhow covered on the server side). Ref: #687 If you really need (check what Rails offers first) special validations, I advise you to create a custom local validator (both server side and client side) as described in this wiki: https://github.com/DavyJonesLocker/client_side_validations/wiki/Custom-Validators and assign it to a specific field. If the custom validation you need is too complex and the above per-field custom validations are still not enough, consider using a standard jQuery callback when all validations pass and rails is ready to submit the form: $(document).on('submit', '#my_form', function(e) {
if (!myValidationFunction()) {
e.preventDefault();
myShowErrorFunction();
// Enable buttons disabled with `disable_with` option
setTimeout(function() {
$.rails.enableElement(e.target);
}, 26)
return false;
}
myHideErrorFunction();
}) Hope it helps Closing here, but feel free to comment |
@tagliala thank you for reply! 'undefined method `-' for nil:NilClass’ is solved! For me, this wiki is easy to understand and your offer, create a custom validator, is difficult for me. As far as I know, phone, zip code, name or something like that use several fields in form and only 1 table column is used in most cases. Is there any easy approach to solve this problem at this awesome gem? |
You should avoid that What you need to validate? |
Hello! First of all I am very sorry because I was very confused. What I need to validate is zip code, name, phone number .... but my problem is solved. |
You're welcome, glad you've solved |
I'm a newbie using rails.
First of all thank you for your client_side_validations!
This is very useful.
The multiple-fields wiki page is good for rails5?
https://github.com/DavyJonesLocker/client_side_validations/wiki/Shared-validations-for-multiple-fields
I tried and get error message like 'undefined method `-' for nil:NilClass' at below code.
I can't judge this is because of rails version or not but I'm worried about published date which is 2012...
The text was updated successfully, but these errors were encountered: