Active Record Validations 4 Strict Validations 严格校验

version: Rails 4.1
 
严格校验
 
你也可以指定校验为严格的,当对象无效时,抛出异常 ActiveModel::StrictValidationFailed。
 
class Person < ActiveRecord::Base
   validates :name , presence: { strict: true }
end
 
Person. new .valid?  # => ActiveModel::StrictValidationFailed: Name can't be blank

也可以把定制化的异常传入到 :strict 选项中。

 
class Person < ActiveRecord::Base
   validates :token , presence: true , uniqueness: true , strict: TokenGenerationException
end
 
Person. new .valid?  # => TokenGenerationException: Token can't be blank
 

 

你可能感兴趣的:(validation,Ruby,Rails)