Skip to content
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

Closed
no1-knows opened this issue Aug 11, 2019 · 5 comments
Closed

Multiple fields validations on rails5 #765

no1-knows opened this issue Aug 11, 2019 · 5 comments

Comments

@no1-knows
Copy link

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.

def great_small_fields_diff
        great_field - small_field
 end

I can't judge this is because of rails version or not but I'm worried about published date which is 2012...

@tagliala
Copy link
Contributor

tagliala commented Aug 11, 2019

Hi,

'undefined method -' for nil:NilClass'means that you are trying to subtractnilto something else. I think your model does not definegreat_field` or its corresponding field. Please take a look to Rails Activee Record Basics

About the wiki: https://github.com/DavyJonesLocker/client_side_validations/wiki/Shared-validations-for-multiple-fields

Quoting from the wiki:

For example you want the value of a field to be greater than another

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

@no1-knows
Copy link
Author

@tagliala thank you for reply!

'undefined method `-' for nil:NilClass’ is solved!
But wiki way doesn’t work because error don’t show up at diplay:none object.

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.
So I hope not to divide these table column.

Is there any easy approach to solve this problem at this awesome gem?

@tagliala
Copy link
Contributor

But wiki way doesn’t work because error don’t show up at diplay:none object.

You should avoid that display:none form field 😅

What you need to validate?

@no1-knows
Copy link
Author

no1-knows commented Aug 14, 2019

Hello!

First of all I am very sorry because I was very confused.
I thought attr_accessor validation doesn't work.
But that was completely wrong after checking new environment.

What I need to validate is zip code, name, phone number .... but my problem is solved.
Thank you for your support!

@tagliala
Copy link
Contributor

You're welcome, glad you've solved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants