跳至內容 跳至搜尋

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 物件初始化的,因此您不需要手動執行此動作。

命名空間
方法
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"]}>
# File activemodel/lib/active_model/validations.rb, line 328
def errors
  @errors ||= Errors.new(self)
end

freeze()

# 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
# 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
# File activemodel/lib/active_model/validations.rb, line 361
def valid?(context = nil)
  current_context = validation_context
  context_for_validation.context = context
  errors.clear
  run_validations!
ensure
  context_for_validation.context = current_context
end

驗證(context = nil)

縮寫為:valid?

驗證!(context = nil)

執行指定內容中的所有驗證。如果未找到錯誤,則返回 true,否則會引發 ValidationError

驗證如果沒有 :on 選項,將在不論內容的情況下執行。驗證如果具有 :on 選項,則只會在指定的內容中執行。

# File activemodel/lib/active_model/validations.rb, line 417
def validate!(context = nil)
  valid?(context) || raise_validation_error
end

驗證(搭配)(*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,詳情請參閱此方法的類別版本。

# File activemodel/lib/active_model/validations/with.rb, line 144
def validates_with(*args, &block)
  options = args.extract_options!
  options[:class] = self.class

  args.each do |klass|
    validator = klass.new(options.dup, &block)
    validator.validate(self)
  end
end

驗證內容()

執行驗證時回傳內容。

執行驗證(但非特定內容)時(與 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
# File activemodel/lib/active_model/validations.rb, line 454
def validation_context
  context_for_validation.context
end

實例私有方法

引發驗證錯誤()

# File activemodel/lib/active_model/validations.rb, line 478
def raise_validation_error # :doc:
  raise(ValidationError.new(self))
end