Class: FormModel::AssociatedValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/form_model/associated_validation.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(form, attribute, value) ⇒ Object

Validates that the associations provided are either all nil or all valid. If neither is true then the appropriate errors will be added to the parent document.

Examples:

Validate the association.

validator.validate_each(document, :name, name)

Parameters:

  • document (Document)

    The document to validate.

  • attribute (Symbol)

    The relation to validate.

  • value (Object)

    The value of the relation.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/form_model/associated_validation.rb', line 15

def validate_each(form, attribute, value)
  begin
    valid = Array.wrap(value).collect do |doc|
      if doc.nil?
        true
      else
        doc.valid?
      end
    end.all?
  end
  form.errors.add(attribute, :invalid, options) unless valid
end