judgegem / judge

Client-side form validation for Rails
MIT License
256 stars 41 forks source link

Validate uniqueness always failing after one fail #24

Closed ricardodovalle closed 10 years ago

ricardodovalle commented 10 years ago

I am trying to use the gem judge and uniqueness validation, but sometimes I am getting a weird error, at most of time, after a validation fail.

After that, the XHR request always return this: ["Judge validation for Person#name not allowed"]

Started GET "/judge?klass=person&attribute=name&value=joao&kind=uniqueness" for 127.0.0.1 at 2014-04-17 01:13:59 -0300
Processing by Judge::ValidationsController#build as JSON
  Parameters: {"klass"=>"person", "attribute"=>"name", "value"=>"joao", "kind"=>"uniqueness"}
Completed 200 OK in 7ms (Views: 0.1ms | ActiveRecord: 0.0ms)

But, if I reload the server and the page, all it will worked fine one or there times, before i change the page or get one uniqueness error (i think)

Thanks,

The code

class Person < ActiveRecord::Base
#person.rb
  validates :name, presence: true, uniqueness: {case_sensitive: false}, length: {minimum: 2, maximum: 200}
  validates :email, uniqueness: true, length: {maximum: 30}, format: {with:/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i}, allow_blank: true
  validates :cpf, presence: true, uniqueness: true, length: {minimum: 10, maximum: 14}, cpf: true
end
#config/initializers/judge.rb
Judge.configure do
  expose Person, :name, :email, :cpf
end
#people.js.coffee
jQuery ->
  $('#person_name').blur ->
    judge.validate document.getElementById("person_name"),
      valid: (element) ->
        element.style.border = "1px solid green"
        $(element).nextAll().remove()
        return

      invalid: (element, messages) ->
        element.style.border = "1px solid red"
        $(element).nextAll().remove()
        $(element).after '<p class=\"text-danger\">' + messages.join(',') + '</p>'
        return
    return
joecorcoran commented 10 years ago

I need to dig into this, but I think it's a problem that only happens in development due to Rails' autoloading classes. cf. #16

ricardodovalle commented 10 years ago

I will try in production, thank you.

ricardodovalle commented 10 years ago

Thanks @joecorcoran, in production it is working very well.

I am using the master version

gem 'judge', git: 'git://github.com/joecorcoran/judge.git'

What version would you recommend to use?

joecorcoran commented 10 years ago

Use the latest version on Rubygems unless you are relying on a bug fix on master, in which case stick with the git option. I should release a new version soon.

Dinuz commented 9 years ago

@ricardodovalle Did you solve this issue? I mean in development, not in production.

@joecorcoran the new version that you were mentioning in the last post (may 15), was released? and if it was released, it solve the problem in development mode too?

Thanks.

rubyconvict commented 9 years ago

Not sure about production env, but it sure is still broken in develpment mode, I have seen other issue for this closed. I am using 'master' commit e79cda4393358630b917ca94d1a4c24e71ff8832.

I think this is a 'Singleton' usage problem, look here:

    def expose(klass, *attributes)
      attrs = (@@exposed[klass] ||= [])
      attrs.concat(attributes).uniq!
    end

This is a workaround that works for me (in development):

before, fragile:

Judge.configure do
  expose User, :username
end

after, solid:

Judge.config.exposed[User] = [:username]
efmiglioranza commented 9 years ago

Same error here. Tried to change it as @rubyconvict suggested, but the error still occurs in development and production mode.

Edit: actually there was a typo in my params list. Seems to be ok now with @rubyconvict suggestion, but the duplicated field error message doesn't appear anymore.

rubyconvict commented 9 years ago

Glad I could help.