How To Create Validation Error Messages In Laravel

admin_img Posted By Bajarangi soft , Posted On 17-09-2020

After calling the errors method on a Validator instance, you will receive an Illuminate\Support\MessageBag instance, which has a variety of convenient methods for working with error messages. The $errors variable that is automatically made available to all views is also an instance of the MessageBag class.

How To Create Validation Error Messages In Laravel

Retrieving The First Error Message For A Field

To retrieve the first error message for a given field, use the first method:
$errors = $validator->errors();

echo $errors->first('email');
 

Retrieving All Error Messages For A Field

If you need to retrieve an array of all the messages for a given field, use the get method:
 

foreach ($errors->get('email') as $message) {
    //
}


If you are validating an array form field, you may retrieve all of the messages for each of the array elements using the * character:

foreach ($errors->get('attachments.*') as $message) {
    //
}


Retrieving All Error Messages For All Fields

To retrieve an array of all messages for all fields, use the all method:

foreach ($errors->all() as $message) {
    //
}


Determining If Messages Exist For A Field

The has method may be used to determine if any error messages exist for a given field:

if ($errors->has('email')) {
    //
}


Custom Error Messages

If needed, you may use custom error messages for validation instead of the defaults. There are several ways to specify custom messages. First, you may pass the custom messages as the third argument to the Validator::make method:

$messages = [
    'required' => 'The :attribute field is required.',
];

$validator = Validator::make($input, $rules, $messages);


In this example, the :attribute placeholder will be replaced by the actual name of the field under validation. You may also utilize other placeholders in validation messages. For example:

$messages = [
    'same' => 'The :attribute and :other must match.',
    'size' => 'The :attribute must be exactly :size.',
    'between' => 'The :attribute value :input is not between :min - :max.',
    'in' => 'The :attribute must be one of the following types: :values',
];


Specifying A Custom Message For A Given Attribute

Sometimes you may wish to specify a custom error message only for a specific field. You may do so using "dot" notation. Specify the attribute's name first, followed by the rule:
 

$messages = [
    'email.required' => 'We need to know your e-mail address!',
];


Specifying Custom Messages In Language Files

In most cases, you will probably specify your custom messages in a language file instead of passing them directly to the Validator. To do so, add your messages to custom array in the resources/lang/xx/validation.php language file.

'custom' => [
    'email' => [
        'required' => 'We need to know your e-mail address!',
    ],
],


Specifying Custom Attribute Values

If you would like the :attribute portion of your validation message to be replaced with a custom attribute name, you may specify the custom name in the attributes array of your resources/lang/xx/validation.php language file:

'attributes' => [
    'email' => 'email address',
],


You may also pass the custom attributes as the fourth argument to the Validator::make method:

$customAttributes = [
    'email' => 'email address',
];

$validator = Validator::make($input, $rules, $messages, $customAttributes);
 

Specifying Custom Values In Language Files

Sometimes you may need the :value portion of your validation message to be replaced with a custom representation of the value. For example, consider the following rule that specifies that a credit card number is required if the payment_type has a value of cc:
$request->validate([
    'credit_card_number' => 'required_if:payment_type,cc'
]);
 

If this validation rule fails, it will produce the following error message:

The credit card number field is required when payment type is cc.

Instead of displaying cc as the payment type value, you may specify a custom value representation in your validation language file by defining a values array:
 

'values' => [
    'payment_type' => [
        'cc' => 'credit card'
    ],
],


Now if the validation rule fails it will produce the following message:
 
The credit card number field is required when payment type is credit card.

Related Post