class Jennifer::Model::Errors

Overview

Container that you can include in your object for handling error messages.

An example of a minimal implementation could be:

class Post
  include Jennifer::Model::Translation

  property title : String?
  getter errors

  def initialize
    @errors = Jennifer::Model::Errors.new(self)
  end

  def validate
    errors.add(:title, :blank) if title.nil?
  end

  # The following method is needed to be minimally implemented

  def self.superclass; end
end

The last method in the described class is required to be implemented to allow Jennifer::Model::Errors to correctly work with class translation lookup. nil return value presents that class has no lookup.

Defined in:

jennifer/model/errors.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(base) #

[View source]

Instance Method Detail

def [](attribute : Symbol) #

When passed a symbol or a name of a method, returns an array of errors for the method.


[View source]
def []?(attribute : Symbol) #

[View source]
def add(attribute : Symbol, message : String | Symbol = :invalid, count : Int? = nil, options : Hash = {} of String => String) #

Adds message to the error messages and used validator type to details on attribute.

More than one error can be added to the same attribute. If no message is supplied, :invalid is assumed.


[View source]
def add(attribute : Symbol, message : String | Symbol = :invalid, options : Hash = {} of String => String) #

[View source]
def any? #

[View source]
def blank? #
Description copied from class Object

Returns if object is blank one - empty (or whitespaced) string, empty array, empty hash, nil or false.


[View source]
def clear #

Clear the error messages.


[View source]
def clone #

Returns a copy of self with all instance variables cloned.


[View source]
def delete(key : Symbol) #

Delete messages for key. Returns the deleted messages.


[View source]
def each(&) #

Iterates through each error key, value pair in the error messages hash.

Yields the attribute and the error for that attribute. If the attribute has more than one error message, yields once for each error message.


[View source]
def empty? #

Returns true if no errors are found, false otherwise. If the error message is a string it can be empty.


[View source]
def full_message(attribute : Symbol, message : String) #

Returns a full message for a given attribute.


[View source]
def full_messages #

Returns all the full error messages in an array.


[View source]
def full_messages_for(attribute : Symbol) #

Returns all the full error messages for a given attribute in an array.


[View source]
def generate_message(attribute : Symbol, message : Symbol, count, options : Hash) #

Translates an error message in its default scope


[View source]
def generate_message(attribute : Symbol, message : String, count, options : Hash) #

[View source]
def include?(attribute : Symbol) #

Returns whether error messages include an error for the given key attribute.


[View source]
def inspect(io) : Nil #

[View source]
def keys #

Returns all message keys.


[View source]
def size #

Returns the number of error messages.


[View source]
def to_a #

[View source]
def values #

Returns all message values.


[View source]