Rails 3:client_side_validations gem 和设计密码验证 [英] Rails 3: client_side_validations gem and devise password validations

查看:16
本文介绍了Rails 3:client_side_validations gem 和设计密码验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 client_side_validations gem 在我的表单上工作得非常好.我正在为我的用户模型使用设计,当我转到 edit_user_registration 表单时,除了 :current_password、:password 和 :password_confirmation 字段之外的所有内容都会抛出验证.

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.

例如,我将电子邮件留空,当我跳出该字段时,验证会立即弹出.但是,如果我将 current_password 字段留空并从其中退出,则什么也不会发生,那么当我提交表单时,我会收到错误消息1 个错误禁止保存此用户:密码不能为空"

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"

感谢您的帮助

http://pastie.org/1907464

推荐答案

目前 ClientSideValidations 将过滤掉任何条件验证器.Devise 将一些验证器设置为有条件的:https://github.com/plataformatec/devise/blob/master/lib/devise/models/validatable.rb#L24-32

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 } %>

<%= 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.

这篇关于Rails 3:client_side_validations gem 和设计密码验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆