Class: Lipstick::Helpers::FormValidationBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/lipstick/helpers/form_validation_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(sym) ⇒ FormValidationBuilder

Returns a new instance of FormValidationBuilder.



6
7
8
9
10
# File 'lib/lipstick/helpers/form_validation_builder.rb', line 6

def initialize(sym)
  @rules = {}
  @messages = {}
  @sym = sym
end

Instance Method Details

#auto_validate(obj, *fields) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/lipstick/helpers/form_validation_builder.rb', line 26

def auto_validate(obj, *fields)
  unless obj.class.respond_to?(:lipstick_auto_validators)
    raise("#{obj.class.name} does not include Lipstick::AutoValidation")
  end

  validators = obj.class.lipstick_auto_validators
  validators.slice(*fields).each do |field, opts|
    validate_field(field, opts)
  end
end

#to_hObject



12
13
14
# File 'lib/lipstick/helpers/form_validation_builder.rb', line 12

def to_h
  { rules: @rules, messages: @messages }
end

#validate_field(field, opts) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/lipstick/helpers/form_validation_builder.rb', line 16

def validate_field(field, opts)
  target = wrap_name(field)

  opts.each do |k, v|
    next validate_with_hash(target, k, v) if v.is_a?(Hash)

    validate_with_param(target, k, v)
  end
end