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
 
original url:  http://guides.rubyonrails.org/active_record_validations.html#strict-validations

 

你可能感兴趣的:(rails,ruby,validation)