Generated 'data-validate' cannot be parsed due to custom regex
Closed this issue · 7 comments
I don't know why and what's cause this bug, but one of my form fails at the validation basically because I got a javascript error.
Uncaught SyntaxError: Unexpected token /
At judge.js:194:
var ValidationQueue = judge.ValidationQueue = function(element) {
this.element = element;
this.validations = [];
this.attrValidators = root.JSON.parse(this.element.getAttribute('data-validate'));
...
The form email input.
<input class="string email required form-control" data-validate="[{"kind":"presence","options":{},"messages":{"blank":"skal udfyldes"}},{"kind":"format","options":{"with":/^\s*(([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})[\s\/,;]*)+$/i},"messages":{"invalid":"er ikke gyldig","blank":"skal udfyldes"}}]" id="user_email" name="user[email]" size="50" type="email" value="">
I think you're missing something with custom regex validation converted into json, looks like something should be protected to avoid this behavior.
Something odd has happened there, the with
regexp in the JSON is not quoted for some reason.
Judge relies on Hash#to_json to dump validator options into the data-validate
attribute. Regular expressions are not part of the JSON standard, but working with JSON in Ruby generally throws up a lot of non-standard behaviour. I suspect that some JSON library is screwing it up.
Please do the following:
- Tell me what versions of Rails and Ruby you are using
- Do the following in your Rails dev console and post the output:
JSON.dump(:with => /abc/)
- Check your Gemfile.lock for any gem that depends on a JSON library.
gem 'rails', '3.2.17'
Ruby: 2.1.0
[1] pry(main)> JSON.dump(:with => /abc/)
=> "{\"with\":/abc/}"
I have 13 match for json
in my gemfile.lock, should I list them all? The
file contains 750 line of code. (Big project)
Okay so Ruby 2.1.0 behaves as follows:
irb(main):003:0> require 'json'
=> true
irb(main):004:0> JSON.dump(:with => /abc/)
=> "{\"with\":\"(?-mix:abc)\"}"
So one of your app's other dependencies is overriding JSON with a class that dumps regular expressions incorrectly. Please list all the JSON entries from your Gemfile.lock and we can work out which one causes the problem.
We found the gem that changed the way the JSOn is rendered. It's ClientSideValidation
at https://github.com/bcardarella/client_side_validations
We can't remove it from our application, I don't really know what to do now.
Ah yes. Out of interest, why are you using that gem and Judge at the same time? They both have the same goal, no?
Exactly, they do the same, I'm currently working on the next version of the
application and the old version need it. So I'm with two different version
using different assets/gems/view/controller until the next major release.
But at the end, I plan on remove this gem that is not maintained anymore,
that's also the reason why I change. Need something that evolves.
I fixed the issue by deleting the crapy gem from the project. Some new changes made that I can now do without it. Otherwise I don't know what I would have done fix this...
Thanks!