Ruby on Rails 密码验证 [英] Ruby on Rails Password Validation

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

问题描述

所以我有有趣的密码验证要求:

So I have interesting password validation requirements:

  • 当用户注册时,我希望他们必须输入密码并确认并且在 6..40 之间(让这个工作 100%)

  • When a user signs up, I want them to have to type in password and confirm and be between 6..40 (GOT THIS WORKING 100%)

当用户更新他们的个人资料时,同样的验证规则适用(100% 成功)

When a user updates their profile, the same validation rules apply (GOT THIS WORKING 100%)

当管理员添加用户时,他们只需要输入一次密码并且密码应该被验证(NOT WORKIG)

When an admin adds a user, they only have to enter the password once and it should be validated (NOT WORKIG)

当管理员编辑用户且密码字段为空时,不应更新密码,如果他们键入内容,则应进行验证.(部分工作)

When an admin edits a user and the password field is blank, it shouldn't update the password, if they type something, it should be validated. (PARTIAL WORKING)

validates :password, :presence => true,
                   :confirmation => true,
                   :length => {:within => 6..40},
                   :unless => :force_submit

我无法涵盖的唯一情况是,当管理员添加用户时,该用户未经过验证,当管理员编辑用户(并输入密码)时,它未经过验证.

The only cases I can't cover are when an admin adds a user, it is not validated and when an admin edits a user (and types in a password) it is not validated.

:force_submit 是从管理表单传入的,因此不会验证密码.(所以更新空密码的情况有效)

the :force_submit is passed in from the admin form, so the password isn't validated. (So the case of an updating empty password works)

有什么想法/魔法吗?

推荐答案

以下似乎符合我的要求...实际上我现在要求所有用户确认...(这使视图更清晰).但是在更新时我允许空白.

The below seem to meet my requirements...I am actually now requiring a confirmation for all users.. (It makes the view cleaner). But on an update I am allowing blanks.

  validates :password, :presence => true,
                       :confirmation => true,
                       :length => {:within => 6..40},
                       :on => :create
  validates :password, :confirmation => true,
                       :length => {:within => 6..40},
                       :allow_blank => true,
                       :on => :update

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

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