@author Guy van den Berg @since 0.9
# File lib/dm-validations/validation_errors.rb, line 40 def self.default_error_message(key, field, *values) field = DataMapper::Inflector.humanize(field) @@default_error_messages[key] % [field, *values].flatten end
Holds a hash with all the default error messages that can be replaced by your own copy or localizations.
# File lib/dm-validations/validation_errors.rb, line 36 def self.default_error_messages=(default_error_messages) @@default_error_messages = default_error_messages end
# File lib/dm-validations/validation_errors.rb, line 123 def [](property_name) if (property_errors = errors[property_name.to_sym]) property_errors end end
Add a validation error. Use the field_name :general if the errors does not apply to a specific field of the Resource.
@param [Symbol] field_name
The name of the field that caused the error
@param [String] message
The message to add
# File lib/dm-validations/validation_errors.rb, line 65 def add(field_name, message) # see 6abe8fff in extlib, but don't enforce # it unless Edge version is installed if message.respond_to?(:try_call) # DM resource message = if (resource.respond_to?(:model) && resource.model.respond_to?(:properties)) message.try_call( resource, resource.model.properties[field_name] ) else # pure Ruby object message.try_call(resource) end end (errors[field_name] ||= []) << message end
Clear existing validation errors.
# File lib/dm-validations/validation_errors.rb, line 53 def clear! errors.clear end
# File lib/dm-validations/validation_errors.rb, line 105 def each errors.each_value do |v| yield(v) unless DataMapper::Ext.blank?(v) end end
# File lib/dm-validations/validation_errors.rb, line 111 def empty? @errors.all? { |property_name, errors| errors.empty? } end
Collect all errors into a single list.
# File lib/dm-validations/validation_errors.rb, line 86 def full_messages errors.inject([]) do |list, pair| list += pair.last end end
# File lib/dm-validations/validation_errors.rb, line 115 def method_missing(meth, *args, &block) errors.send(meth, *args, &block) end
Return validation errors for a particular field_name.
@param [Symbol] field_name
The name of the field you want an error for.
@return [Array<DataMapper::Validations::Error>]
Array of validation errors or empty array, if there are no errors on given field
# File lib/dm-validations/validation_errors.rb, line 100 def on(field_name) errors_for_field = errors[field_name] DataMapper::Ext.blank?(errors_for_field) ? nil : errors_for_field.uniq end
Generated with the Darkfish Rdoc Generator 2.