【发布时间】:2015-11-13 12:28:06
【问题描述】:
如何区分哪个验证器失败了?
我在同一个字段上有多个验证:
class User < ActiveRecord::Base
validates :name, presence: true, length: {minimum: 1, maximum: 20 }
validates_uniqueness_of :name
end
当我将用户保存为 user.save - 我想区分失败的原因。
if user.__not_valid_name_length__?
# name length wrong
# do smth 1
end
if **user.__not_valid_name_unique__?
# name is not unique
# do smth 2
end
我可以访问 user.errors[:name] 并查看该字段的所有错误消息。 但我不想依赖可以更改的消息文本。
有没有办法知道哪个验证器失败了?
【问题讨论】: