module FreeForm::Validation

Public Class Methods

included(base) click to toggle source
# File lib/freeform/form/validation.rb, line 3
def self.included(base)
  base.extend(ClassMethods)
  extend FreeForm::Property
end

Protected Instance Methods

validate_component_models() click to toggle source
# File lib/freeform/form/validation.rb, line 23
def validate_component_models
  models.each { |m| validate_model(m) unless m.is_a?(FreeForm::Form) }
end
validate_nested_forms() click to toggle source
# File lib/freeform/form/validation.rb, line 19
def validate_nested_forms
  models.each { |m| validate_nested_form(m) if m.is_a?(FreeForm::Form) }
end

Private Instance Methods

add_error_to_form(model, error, message) click to toggle source
# File lib/freeform/form/validation.rb, line 51
def add_error_to_form(model, error, message)
  field = find_form_field_from_model_field(model, error)
  if field
    errors.add(field, message)
  end
end
add_model_errors_to_form(model) click to toggle source
# File lib/freeform/form/validation.rb, line 45
def add_model_errors_to_form(model)
  model.errors.each do |error, message|
    add_error_to_form(model, error, message)
  end
end
find_form_field_from_model_field(model, error_field) click to toggle source
# File lib/freeform/form/validation.rb, line 58
def find_form_field_from_model_field(model, error_field)
  model_property_mappings.each_pair do |property, attributes|
    model_sym = model_symbol_from_model(model)
    field_sym = error_field.to_sym

    if (attributes[:model] == model_sym) && (attributes[:field] == field_sym)
      return property
    end
  end
  nil
end
form_marked_for_destruction?(form) click to toggle source
# File lib/freeform/form/validation.rb, line 41
def form_marked_for_destruction?(form)
  form.respond_to?(:marked_for_destruction?) ? form.marked_for_destruction? : false
end
marked_for_destruction_or_valid?(form) click to toggle source
# File lib/freeform/form/validation.rb, line 37
def marked_for_destruction_or_valid?(form)
  form_marked_for_destruction?(form) || form.valid?
end
model_symbol_from_model(model) click to toggle source
# File lib/freeform/form/validation.rb, line 70
def model_symbol_from_model(model)
  models_as_symbols.find { |sym| return sym if send(sym) == model }
end
models_as_symbols() click to toggle source
# File lib/freeform/form/validation.rb, line 74
def models_as_symbols
  self.class.models.map { |x| x.to_sym }
end
validate_model(model) click to toggle source
# File lib/freeform/form/validation.rb, line 32
def validate_model(model)
  model.valid?
  add_model_errors_to_form(model)
end
validate_nested_form(form) click to toggle source
# File lib/freeform/form/validation.rb, line 28
def validate_nested_form(form)
  errors.add(:base, "has invalid nested forms") unless marked_for_destruction_or_valid?(form)
end