grimen / validatious-on-rails

Rails plugin that maps model validations to class names on form elements to integrate with Validatious. IMPORTANT: Project is not maintained anymore.
MIT License
346 stars 13 forks source link

Ajax Validations #24

Open resistorsoftware opened 14 years ago

resistorsoftware commented 14 years ago

Hi,

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? Thanks

Thanks

grimen commented 14 years ago

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.