Skip to content

Instantly share code, notes, and snippets.

@SabrinaMarkon
Last active August 22, 2016 19:24
Show Gist options
  • Save SabrinaMarkon/b43b11eb7254262c052b1beedd1ef30a to your computer and use it in GitHub Desktop.
Save SabrinaMarkon/b43b11eb7254262c052b1beedd1ef30a to your computer and use it in GitHub Desktop.
Sabrina Notes - Laravel 5.2 - Validator: How to customize the error messages produced by the validator
Sabrina Notes - Laravel 5.2 - Validator: How to customize the error messages produced by the validator
-------------------------------------------------------------------------------------------------------
1) Edit /resources/lang/en/validation.php
2) In the first section, called 'Validation Language Lines', you can edit the default error messages for existing validation rules:
Example:
One default error message is:
'alpha_num' => 'The :attribute may only contain letters and numbers.',
You can change that to be, say:
'alpha_num' => 'The :attribute may only contain alphanumeric characters.',
3) In the second section of validation.php, called 'Custom Validation Language Lines', customize the 'rule-name' error for an attribute called 'attribute-name' if you would like to create your own special validation rules:
'custom' => [
'attribute-name' => [
'rule-name' => 'custom-message',
],
],
Example:
'custom' => [
'firstname' => [
'alpha_num_and_dash' => 'The first name field must consist only of letters, numbers, or dashes',
],
],
4) For errors such as those produced by the 'required' rule (and others), the default error message
uses the name of the field and says things like 'The firstname field is required'
To make it sound more user-friendly and professional, such as 'The First Name field is required',
in the same /resources/lang/en/validation.php file, the bottom section allows you to list the substitions under the
Custom Validation Attributes section:
'attributes' => [],
Example:
'attributes' => [
'firstname' => 'First Name',
'lastname' => 'Last Name',
'email' => 'Email Address',
],
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment