Validation Rails 4验证使用消息;太长了;而不是;“简称”;

Validation Rails 4验证使用消息;太长了;而不是;“简称”;,validation,ruby-on-rails-4,Validation,Ruby On Rails 4,当用户想要更改其密码时,我使用一个范围来定义密码的长度: validates :password, confirmation: true, length: { in: 6..20, too_short: "to short message", too_long: "too long message"} 但如果输入太短,则错误消息太长,为什么? 我通过config/models/locales/en.yml用于翻译I18n,错误是一样的,我使用这个例

当用户想要更改其密码时,我使用一个范围来定义密码的长度:

  validates :password, confirmation: true, length: {
      in: 6..20, 
      too_short: "to short message",
      too_long: "too long message"}
但如果输入太短,则错误消息太长,为什么?


我通过config/models/locales/en.yml用于翻译I18n,错误是一样的,我使用这个例子是因为es更容易解释

问题是我使用了BCrypt,它在密码字段中存储了一个大于20个字符的加密值

因此,解决方案是将所有验证移动到密码确认字段:

validates :password, confirmation: true
validates :password_confirmation, presence: true, length: {in: 6..20}, if: "verify_password==0"
现在,如果我输入的值小于6个字符大小,我会得到一个正确的错误


为什么不直接使用单个消息包含:{in:6..20,消息:'应该在6..20'范围内}。。事实上,我不使用在线消息,而是在locale/en.yml文件中使用I18n进行翻译