Class: RequiredValidationRule

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



223
224
225
226
227
228
229
230
231
# File 'lib/validation_profiler/rules/rules.rb', line 223

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 required"
  else
    attributes[:message]
  end
end

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



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/validation_profiler/rules/rules.rb', line 234

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

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

  if field_value == nil
    return false
  end

  if field_value.is_a?(Fixnum)
    return true
  end

  return !field_value.empty?

end