class
Marten::Core::Validation::ErrorSet
- Marten::Core::Validation::ErrorSet
- Reference
- Object
Overview
Represents a set of validation errors.
Included Modules
Defined in:
marten/core/validation/error_set.crmarten/template/ext/marten/core/validation/error_set.cr
Constant Summary
-
DEFAULT_ERROR_TYPE =
:invalid
Constructors
Instance Method Summary
-
#[](field : Nil | String | Symbol)
Returns all the errors associated with a specific field.
-
#add(field : String | Symbol, message : String, *, type : Nil | String | Symbol = nil)
Adds a new field error to the error set.
-
#add(message : String, *, type : Nil | String | Symbol = nil)
Adds a new global error to the error set.
-
#clear(*args, **options)
Clears all the
Marten::Core::Validation::Errorobjects from this error set. -
#clear(*args, **options, &)
Clears all the
Marten::Core::Validation::Errorobjects from this error set. -
#each(*args, **options)
Iterates over all the
Marten::Core::Validation::Errorobjects in this error set. -
#each(*args, **options, &)
Iterates over all the
Marten::Core::Validation::Errorobjects in this error set. -
#global
Returns the global errors (errors that are not associated with a specific field).
-
#size(*args, **options)
Returns the number of errors.
-
#size(*args, **options, &)
Returns the number of errors.
-
#to_json(json : JSON::Builder) : Nil
Serializes the error set to JSON.
Macros inherited from module Marten::Template::CanDefineTemplateAttributes
template_attributes(*names)
template_attributes
Constructor Detail
Instance Method Detail
Adds a new field error to the error set.
This method can be used to add a new error associated with a specific field to an existing error set. At least
a field and a message must be specified:
record.errors.add(:attribute, "This record attribute is invalid")
Optionally, an error type can be explicitly specified, otherwise it defaults to :invalid:
records.errors.add(:attribute, "This record attribute is invalid!", type: :invalid_attribute)
Adds a new global error to the error set.
This method can be used to add a new global error (that is not associated with a specific field) to an
existing error set. At least a message must be specified:
record.errors.add("This record is invalid!")
Optionally, an error type can be explicitly specified, otherwise it defaults to :invalid:
record.errors.add("This record is invalid!", type: :invalid_record)
Clears all the Marten::Core::Validation::Error objects from this error set.
Clears all the Marten::Core::Validation::Error objects from this error set.
Iterates over all the Marten::Core::Validation::Error objects in this error set.
Iterates over all the Marten::Core::Validation::Error objects in this error set.
Serializes the error set to JSON.
The generated value is an array of objects. Each object contains the field, type, and message
attributes of one error. field is null when the error is global.