Search code examples
ruby-on-rails-3deviseclient-side-validation

Rails 3: client_side_validations gem and devise password validations


I've got the client_side_validations gem working really great on my forms. I'm using Devise for my users model and when I go to the edit_user_registration form, validations are thrown up for everything except the :current_password, :password, and :password_confirmation fields.

Fort instance is I leave the email blank, the validation pops up right when I tab out of the field. However, if I leave the current_password field blank and tab out of it nothing happen, then when I submit the form I get the error "1 error prohibited this user from being saved: password can't be blank"

Thanks for any help

http://pastie.org/1907464


Solution

  • Currently ClientSideValidations will filter out any conditional validators. Devise sets some of the validators as conditional: https://github.com/plataformatec/devise/blob/master/lib/devise/models/validatable.rb#L24-32

    The reason I did this is because there is no good way for the client to determine the true value of that conditional. I could do it at the time the form is generated but what if that conditional relied upon a value that could be changed on the form? So I opted to filter them and let things fall back to the server.

    That was the idea but clearly it has imposed unfair limitations on some things. This being the most obvious (and popular).

    So I plan on releasing a new version very soon that will allow you to explicitly override the conditional filters. It will work like this:

    <%= f.text_field :password, :validate => { :presence => true, :confirmation => true } %>
    

    or

    <%= f.text_field :password, :validate => true %>
    

    In the first case you can choose which validators to turn the filter off. In the 2nd case it will turn the filter off for all validators on that attribute. The conditional will be evaluated at the time the form is generated and if it passes it will add the validator to the input element for use on the client.