Active Model Validations
提供完整的驗證框架給您的物件。
一個最小的實作可以是
class Person
include ActiveModel::Validations
attr_accessor :first_name, :last_name
validates_each :first_name, :last_name do |record, attr, value|
record.errors.add attr, "starts with z." if value.start_with?("z")
end
end
它提供給您由 Active Record 所知的完整標準驗證堆疊
person = Person.new
person.valid? # => true
person.invalid? # => false
person.first_name = 'zoolander'
person.valid? # => false
person.invalid? # => true
person.errors.messages # => {first_name:["starts with z."]}
請注意 ActiveModel::Validations
自動將一個 errors
方法新增至您的實例,而該實例是由一個新的 ActiveModel::Errors
物件初始化的,因此您不需要手動執行此動作。
命名空間
- 模組 ActiveModel::Validations::Callbacks
- 模組 ActiveModel::Validations::ClassMethods
- 模組 ActiveModel::Validations::HelperMethods
- 類別 ActiveModel::Validations::AcceptanceValidator
方法
- E
- F
- I
- R
- V
包含的模組
實例的公有方法
errors() 連結
傳回包含所有屬性錯誤訊息的 Errors
物件。
class Person
include ActiveModel::Validations
attr_accessor :name
validates_presence_of :name
end
person = Person.new
person.valid? # => false
person.errors # => #<ActiveModel::Errors:0x007fe603816640 @messages={name:["can't be blank"]}>
來源:顯示 | 在 GitHub 上
# File activemodel/lib/active_model/validations.rb, line 328 def errors @errors ||= Errors.new(self) end
freeze() 連結
來源:顯示 | 在 GitHub 上
# File activemodel/lib/active_model/validations.rb, line 372 def freeze errors context_for_validation super end
invalid?(context = nil) 連結
執行 valid?
的相反動作。若新增錯誤,則回傳 true
,否則回傳 false
。
class Person
include ActiveModel::Validations
attr_accessor :name
validates_presence_of :name
end
person = Person.new
person.name = ''
person.invalid? # => true
person.name = 'david'
person.invalid? # => false
也可以選擇提供 context,以定義要測試哪些回呼(context 是使用 :on
在驗證中定義的)。
class Person
include ActiveModel::Validations
attr_accessor :name
validates_presence_of :name, on: :new
end
person = Person.new
person.invalid? # => false
person.invalid?(:new) # => true
來源:顯示 | 在 GitHub 上
# File activemodel/lib/active_model/validations.rb, line 408 def invalid?(context = nil) !valid?(context) end
valid?(context = nil) 連結
執行所有指定的驗證,如果沒有新增任何錯誤,則回傳 true
,否則回傳 false
。
class Person
include ActiveModel::Validations
attr_accessor :name
validates_presence_of :name
end
person = Person.new
person.name = ''
person.valid? # => false
person.name = 'david'
person.valid? # => true
也可以選擇提供 context,以定義要測試哪些回呼(context 是使用 :on
在驗證中定義的)。
class Person
include ActiveModel::Validations
attr_accessor :name
validates_presence_of :name, on: :new
end
person = Person.new
person.valid? # => true
person.valid?(:new) # => false
別名為:validate
驗證!(context = nil) 連結
執行指定內容中的所有驗證。如果未找到錯誤,則返回 true
,否則會引發 ValidationError
。
驗證(搭配)(*args, &block) 連結
傳遞記錄至指定類別或類別,並允許其基於更複雜的條件添加錯誤。
class Person
include ActiveModel::Validations
validate :instance_validations
def instance_validations
validates_with MyValidator
end
end
關於建立自訂驗證模組的詳細資訊,請參閱類別方法文件。
您也可以傳遞多個類別,如下所示
class Person
include ActiveModel::Validations
validate :instance_validations, on: :create
def instance_validations
validates_with MyValidator, MyOtherValidator
end
end
`validates_with` 類別版本的標準組態選項 (:on
、:if
和 :unless
) 應放置在 validates
方法中,因為它們在呼叫中套用和測試。
如果您傳遞任何額外組態選項,它們將傳遞至類別並可用作 options
,詳情請參閱此方法的類別版本。
驗證內容() 連結
執行驗證時回傳內容。
執行驗證(但非特定內容)時(與 on
選項相反)非常實用。
class Person
include ActiveModel::Validations
attr_accessor :name
validates :name, presence: true, if: -> { validation_context != :custom }
end
person = Person.new
person.valid? #=> false
person.valid?(:new) #=> false
person.valid?(:custom) #=> true