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, parent = nil) ⇒ Object



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/validation_profiler/rules/rules.rb', line 371

def error_message(field, attributes, parent = nil)

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

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

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



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/validation_profiler/rules/rules.rb', line 390

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

  #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