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



245
246
247
248
249
250
251
252
253
# File 'lib/validation_profiler/rules/rules.rb', line 245

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

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



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/validation_profiler/rules/rules.rb', line 256

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

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

  return false

end