Class: ListValidationRule

Inherits:
ValidationRule show all
Defined in:
lib/validation_profiler/rules/rules.rb

Instance Method Summary collapse

Methods inherited from ValidationRule

#get_field_value, #is_required?

Instance Method Details

#error_message(field, attributes = {}, parent = nil) ⇒ Object



538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
# File 'lib/validation_profiler/rules/rules.rb', line 538

def error_message(field, attributes = {}, parent = nil)

  field_name = field.to_s
  if parent != nil
    field_name = "#{parent.to_s}.#{field.to_s}"
  end

  #check if a custom error message has been specified in the attributes
  if attributes[:message] == nil
    #no custom error message has been specified so create the default message.
    "#{field_name} is not an accepted value."
  else
    attributes[:message]
  end
end

#validate(obj, field, attributes = {}, parent = nil) ⇒ Object



555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/validation_profiler/rules/rules.rb', line 555

def validate(obj, field, attributes = {}, parent = nil)

  #attempt to get the field value from the object
  field_value = get_field_value(obj, field)

  if !is_required?(field_value, attributes)
    return true
  end

  if field_value == nil
    return false
  end

  list = attributes[:list]
  if list == nil || !list.is_a?(Array)
    raise InvalidRuleAttributes.new(ListValidationRule, field)
  end

  if !list.include?(field_value)
    return false
  end

  return true

end