Class: Fulcrum::ChoiceListValidator

Inherits:
BaseValidator show all
Defined in:
lib/fulcrum/validators/choice_list_validator.rb

Instance Attribute Summary

Attributes inherited from BaseValidator

#data, #errors

Instance Method Summary collapse

Methods inherited from BaseValidator

#add_error, #initialize, #valid?

Constructor Details

This class inherits a constructor from Fulcrum::BaseValidator

Instance Method Details

#choices(elements) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fulcrum/validators/choice_list_validator.rb', line 14

def choices(elements)
  if elements.kind_of?(Array) && !elements.empty?
    elements.each do |choice|
      if choice.blank?
        add_error('choices', 'choice', 'cannot be empty')
      else
        if choice['label'].blank?
          add_error('choice', 'label', 'is required')
        end
      end
    end
  else
    add_error('choice_list', 'choices', 'must be a non-empty array')
  end
end

#validate!Object



4
5
6
7
8
9
10
11
12
# File 'lib/fulcrum/validators/choice_list_validator.rb', line 4

def validate!
  if data['choice_list'].kind_of?(Hash) && !data['choice_list'].blank?
    add_error('choice_list', 'name', 'must not be blank') if data['choice_list']['name'].blank?
    choices(data['choice_list']['choices'])
  else
    @errors['choice_list'] = ['must be a non-empty hash']
  end
  return valid?
end