开发者

Rails 3: client_side_validations gem and devise password validations

开发者 https://www.devze.com 2023-03-06 07:13 出处:网络
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 everyth

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 pr开发者_JAVA技巧ohibited this user from being saved: password can't be blank"

Thanks for any help

http://pastie.org/1907464


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.


The master branch now supports this format. Point your Gemfile to it and you should be good


It’s simple! The gem extends the Rails default form builder, and all you have to do is set a :validate => true option on any form_for (or simple_form_for for simple form users) tag that you want the inline validations for. The form builder uses some rails reflections on your model validations to generate some json that gets included in a script tag after your form. The json is then used by the gem’s Javascript framework to perform the validations that need to be performed.

<%= form_for(@user, :url => registration_path(resource_name), :validate => true) do |f| %>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号