Validator class
The Validator Class.
The heart of Magic's validation system. Runs rules against data and provides a clean API for validation in controllers.
Usage
final validator = Validator.make({
'email': 'user@example.com',
'password': 'secret',
}, {
'email': [Required(), Email()],
'password': [Required(), Min(8)],
});
if (validator.fails()) {
print(validator.errors());
}
// Or throw on failure:
validator.validate(); // Throws ValidationException if fails
Message Resolution
When a rule fails, the Validator resolves the message:
- Calls
rule.message()to get the key/raw message - If
Lang.has(key), usesLang.get(key, params)for translation - Otherwise uses the message as a raw string
- Replaces
:attributewith the humanized field name
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
errors(
) → Map< String, String> - Get all validation errors.
-
fails(
) → bool - Run the validation and check if it failed.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
passes(
) → bool - Run the validation and check if it passed.
-
toString(
) → String -
A string representation of this object.
inherited
-
validate(
) → Map< String, dynamic> - Run validation and throw if it fails.
-
validateAsync(
) → Future< Map< String, dynamic> > - Run validation including any AsyncRules and throw if it fails.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited