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 = {}) ⇒ Object



477
478
479
480
481
482
483
484
485
# File 'lib/validation_profiler/rules/rules.rb', line 477

def error_message(field, attributes = {})
  #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} is not an accepted value."
  else
    attributes[:message]
  end
end

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



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/validation_profiler/rules/rules.rb', line 488

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

  #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

  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