Class: MatchValidationRule

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



328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/validation_profiler/rules/rules.rb', line 328

def error_message(field, attributes)

  match_field = attributes[:field]

  #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} does not match #{match_field}"
  else
    attributes[:message]
  end
end

#validate(obj, field, attributes) ⇒ Object



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/validation_profiler/rules/rules.rb', line 342

def validate(obj, field, attributes)

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

  match_field = attributes[:field]
  if match_field == nil
    raise InvalidRuleAttributes.new(MatchValidationRule, field)
  end

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

  match_field_value = get_field_value(obj, match_field)

  return field_value == match_field_value

end