Working With Error Messages
After calling the errors
method on the validator, you will receive an ErrorBag
instance, which has a variety of convenient methods for working with error messages.
Let's say we have the following data and rules, and we run the validation.
const validator = make()
.setData({ email: 123 })
.setRules({
name: 'required',
email: 'required|string|email',
})
.validate();
Retrieving the First Error Message For a Field
To retrieve the first error message for a given field, use the first
method.
const errors = validator.errors();
// The email must be a string.
console.log(errors.first('email'));
To retrieve the first message regardless of the attribute, use the first
method without passing any value.
const firstError = errors.first();
// The name field is required.
console.log(firstError);
Check If Error Exists
To check if an error exists for a specific attribute, use the has
method.
if (errors.has('email')) {
//
}
Retrieving All Error Messages For A Field
If you want to retrieve an array of all messages for a given field, use the get
method
const emailErrors = errors.get('email');
// The value of emailErrors will be
[
'The email must be a string.',
'The email must be a valid email address.'
]
If you would like to retrieve the error type linked to each error message, you can pass true
as the second argument to the get
method.
const emailErrors = errors.get('email', true);
// The value of emailErrors should be
[
{
error_type: 'string',
message: 'The email must be a string'
},
{
error_type: 'email',
message: 'The email must be a valid email address.'
}
]
Error Types
If you wish to always get the Error Types without having to pass true
as second argument, you can do so by invoking the addErrorTypes
method.
const errors = validator.errors().addErrorTypes();
Retrieving Error Messages for All the fields
If you want to retrieve the error messages for all the fields, use the all
method.
const errors = errors().all();
// The value of errors should be
{
name: [ 'The name field is required.' ],
email: [
'The email must be a string.',
'The email must be a valid email address.'
]
}
info
The all
method definition is
all(allMessages?: boolean, withErrorTypes?: boolean): object;
If you want only to get the first message related to each field you can pass false
as the first parameter to the all
method.
const errors = errors().all(false);
// The value of errors should be
{
name: 'The name field is required.',
email: 'The email must be a string.'
}
We can get the error types, the same way we did it for the get
method by either sending true as the second argument or by invoking addErrorTypes
method on the errors instance.
const errors = errors().all(true, true);
// The value of errors should be
{
name: [
{ error_type: 'required', message: 'The name field is required.' }
],
email: [
{ error_type: 'string', message: 'The email must be a string.' },
{
error_type: 'email',
message: 'The email must be a valid email address.'
}
]
}
Clear Errors
To clear form errors, use the clearErrors()
method on the validator
instance.
// clear all errors
validator.clearErrors();
// Clear errors for specific fields...
validator.clearErrors('field', 'anotherfield');
Set Errors
If you would like to set errors manually. Specially if you are using the library on the client-side and would like to
set error messages received from the server-side, you can set your own errors use the setErrors()
method
// Set an error for the attributes
validator.setErrors({
name: 'name is not valid',
email: 'email is not valid',
});
// Set multiple errors for the attributes
validator.setErrors({
name: ['name is not valid', 'name should have at least 3 characters'],
email: ['email is not valid', 'email should be a string'],
});