Rails 3: client_side_validations gem and devise password validations

China☆狼群 提交于 2019-11-30 14:22:17

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| %>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!