Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Ajax Validations #24

resistorsoftware opened this Issue Nov 10, 2009 · 1 comment


None yet
2 participants


I have a simple form for user registration, and with Authogic plugin, the acts_as_authentic method magically adds validators to certain fields, like :login, :email and :password. I setup a Formtastic for capturing registrations. The validates_uniqueness_of validator on the :login field causes a GET request to be fired to a route http://localhost/validators/uniqueness?model=foo&attribute=login&value= which is empty.

So, looking into v2.rails.js, it takes care of finding fields with 'uniqueness' specified, and fires off an Ajax GET to what could be a controller named 'validates that responds to an action 'uniqueness'

At this point I am not sure how to write my controller reponse. The XHR request is processed for some model, and some column. In my case say Foo.find_by_login.first would return either some existing row or nil. What should my response be then? I am looking for some example validation response that would make sense here. A uniqueness XHR response for true or false would be very helpful for me here? How do you code that up?

Also, inspecting the request, it does not seem to build itself up as a proper XHR request. There is no indication to the server that this is even Ajax?



grimen commented Nov 10, 2009

Hmm, uniqueness triggers an AJAX request in my test-project - are u sure? Peep the console, it should print out some debugging info in development environment.

The fine thing is that I implemented AJAX validations in a generic way that should work for anyone (using ActiveRecord that is), which means that you don't need to care about the controller logic at all. To get an idea how this works, peep these files:

  1. http://github.com/grimen/validatious-on-rails/blob/master/lib/validatious-on-rails/controller.rb
  2. http://github.com/grimen/validatious-on-rails/blob/master/lib/validatious-on-rails/validatious/remote_validator.rb
  3. http://github.com/grimen/validatious-on-rails/blob/master/lib/validatious-on-rails/validatious/validators/uniqueness_validator.rb

As I mention in README though: AJAX validations (including validates_uniqueness_of) is in alpha-stage. Some tricks needs to be solved (see tickets), but it works about 90% of "as intended". I wouldn't use the AJAX validations in production right now, my goal is to squash these bugs this month though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment