Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

various format-with validators on one page. #30

Open
tec opened this Issue · 8 comments

4 participants

@tec
tec commented

If I understand it correctly, the following line in lib/validatious-on-rails/model_validations.rb adds all the client-side and remote validations to one string and then to the html. The unless in the string inhibits identical validations to be added several times.

validator_js << v.to_js

unless existing_validators.present?
&& /#{v.name}/ =~ existing_validators

Unfortunately I had a case where several validations both had the v.name 'format-with', but with different formats. So one of them was not displayed. i solved it by also matching v.to_class in the unless. v.to_class differs because of the generated (random) params id.

validator_js << v.to_js

unless existing_validators.present?
&& /#{v.name}/ =~ existing_validators
&& /#{v.to_class}/ =~ existing_validators

@grimen
Owner

You only need one format-of validator. What I do is to save the formtats in a "var params", with a lookup key representing the hash of the value, which is used in the input filed class (you'll see a number instead of the expression - because Regexp expressions are not valid XHTML classes). The format-of is generic and is smart enough to lookup the correct RegExp value depending on the class number (hashed value). If this don't work, its probably on of all the tiny bugs I'm not aware of. Does it not work?

@grimen
Owner

To generalize: Validatious-On-Rails tries to only generate one validator per validation type. Only the class attribute and some additional meta data for tricky stuff is different.

@tec
tec commented

Thank you for your very quick replies. Yes, it does not work in my case. I guess the problem is that v.to_js does not only contain the lines that look like

v2.Validator.add(...

but also the lines that look like

v2.rails.params['888330768'] = ...

In my case only the first two where generated, the third was omitted, although necessary:

v2.rails.params['11024396765'] = /^[a-zA-z0-9_]*$/;

v2.rails.params['789355800'] = ["admin","superuser","delete","blabla"];

v2.rails.params['888330768'] = /^[A-Z0-9_.%+-]+@(?:[A-Z0-9-]+.)+(?:[A-Z]{2,4}|museum|travel)$/i;

@grimen
Owner

OK, that's not good. Will need to look on that. Can't do it today for sure, but maybe tomorrow. I tried to avoid patch stuff while I got a huge refactor branch that should be merged in as soon as I get the AJAX controller test to pass, but this fix don't sound too messy. Actually I thought it worked already. =S

@tec
tec commented

Don't mind. There's no hurry. Thanks for looking into it. Let me know if I can provide additional info.

@javierm

I think this is the same bug that is happening to me.

I've got the following validations:

validates_format_of :cif,
  :with => /\A[A-Za-z][0-9]{7}.\z/,
  :allow_blank => true

validates_format_of :ccc,
  :with => /\A[0-9]{20}\z/,
  :allow_blank => true

If I remove one of them, everything works fine. But if I leave both of them, only the first one of them works on blur.

The most annoying part is that in this situation when I click the submit button, validation errors for the "cif" field are shown by javascript, but the form is sent anyway.

Here's the javascript code generated by the code above:

v2.Rails.params['596582051'] = /^[A-Za-z][0-9]{7}.$/;
v2.Validator.add({
  acceptEmpty: false,
  fn: function(field, value, params){
    if (v2.present(params[1]) && v2.bool(params[1]) && v2.empty(value)) {return true;};
    if (v2.present(params[2]) && v2.bool(params[2]) && v2.blank(value)) {return true;};
    if (v2.present(params[2]) && !v2.bool(params[2])) {v2.trimField(field);value = v2.trim(value);};
    return v2.Rails.params[params[0]].test(value);
  },
  message: "no es v\u00e1lido",
  name: "format-with",
  params: ["format","allow_nil","allow_blank"]
});

Thanks.

@grimen
Owner

Yea, a horrible bug. I hope I can get back to this project soon to fix these; working on a big project, so most my open-source is related to my needs for that project since a few months back. =(

@tecnh

This seems to affect you whenever you have two or more regex validations on the same form. For me I was attempting to regex the number and expiry date of a credit card. The patch listed at the top of the issue fixed the problem for me but are there any side effects I should be aware of? (Thanks tec for the fix btw.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.