Class: EmailValidationRule

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

Constant Summary collapse

REGEX =
/^[^@]+@[^@]+\.[^@]+$/

Instance Method Summary collapse

Methods inherited from ValidationRule

#get_field_value, #is_required?

Instance Method Details

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



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/validation_profiler/rules/rules.rb', line 288

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 a valid email address"
  else
    attributes[:message]
  end
end

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



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/validation_profiler/rules/rules.rb', line 305

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

  #validate the value against the regex
  if field_value =~ REGEX
    return true
  end

  return false

end