Custom Laravel validation messages

Custom Laravel validation messages

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Custom Laravel Validation Messages

When building authentication flows or complex forms in Laravel, the default validation error messages like "The :attribute field is required" are often too blunt for a production application. You want to speak directly to your users with specific, localized feedback. However, simply injecting a $messages array into the Validator::make() call often fails to override the defaults. The issue in the provided code snippet stems from how the validator instance is constructed and where the message bag is assigned.

In the original example, the $messages array is defined before the validator is instantiated but is not actually passed as an argument to the constructor. The Validator::make() method accepts a third parameter specifically for this purpose. Without passing it, the validator creates its own internal message bag populated with the default definitions found in your Laravel installation's configuration files.

To fix this, you must explicitly pass the $messages array as the third argument to the Validator constructor. Here is the corrected implementation:

$messages = [
    'username.required' => 'Harap isi bagian :attribute.',
    'unique'            => ':attribute sudah digunakan', // Note: This key is ambiguous, see below
];

$validator = Validator::make($request->all(), [
    'username' => ['required', 'unique:Userlogin,username'],
    'password' => 'required',
    'email'    => ['required', 'unique:Userlogin,email']
], $messages);

There is a critical nuance in the original code regarding how validation rules are keyed within your custom message array. The second example uses 'unique' as the key for the message, but Laravel's validation engine looks for messages using a fully qualified rule format. If you define unique as a top-level key, it will not match the specific error triggered by the unique:Userlogin,username rule on the email field versus the username field.

Laravel expects custom messages to follow the pattern {attribute_name}.{rule_name}. For instance, instead of just 'unique', you should define 'email.unique' and 'username.unique'. If you only provide a generic key like 'unique', it might catch all unique violations, but it's generally safer and more explicit to target specific fields if the message differs between them. However, if your intention is to use a single generic fallback for all unique errors, ensure the rule syntax matches exactly what Laravel generates internally.

Furthermore, when using the fails() method followed by withErrors(), you are correctly utilizing the $errors instance property of the validator. This instance now contains your custom messages because they were passed during initialization. The flow remains:

  1. Instantiate the validator with rules and the custom message bag.
  2. Check if validation fails using fails().
  3. Redirect back to the form while passing the $validator object (or specifically $validator->errors() depending on your version, though passing the object is standard in older Laravel versions) so the view can access the localized messages via $errors.

Configuration and Trade-offs

While passing a message array inline works well for single-purpose controllers, it creates a tight coupling between your logic and your error text. This makes refactoring difficult; changing an error message requires finding every controller where that validation rule exists. A more robust architectural decision is to define these messages in a dedicated file, such as app/Validation.php or within the lang directory using Laravel's localization system.

By moving custom messages into a separate class file and instantiating it via new ValidationMessages(), you decouple the logic from the text. You can then pass this instance to the validator:

$messages = new ValidationMessages(); // Assuming a helper class or factory
// Or better yet, use the lang directory for internationalization

Using the language files in the resources/lang/{locale}/validation.php is the preferred approach for multi-language applications. It allows Laravel to automatically pick up the custom messages based on the user's locale without you having to manually instantiate a message bag in every controller. The inline array method is best reserved for quick prototypes or cases where you need highly dynamic, runtime-specific error texts that wouldn't belong in a static translation file.

Implementation Details

It is also worth noting the version differences in how Validator is accessed. In modern Laravel versions (5.x and 6+), the facade Validator::make() is standard, but using use Illuminate\Support\Facades\Validator; ensures you are calling the correct class regardless of namespace configuration. Additionally, ensure that your form field names match exactly. If your form sends email_address but the validation rule checks for email, Laravel will treat them as two different fields and might not apply your custom message correctly if the key in the $messages array doesn't account for the actual field name used.

Finally, when redirecting back after a failed validation, always ensure you pass the input data using withInput(). Without this, the form will reset to an empty state, forcing users to re-enter their data even though they only made a minor mistake like leaving the password blank. Combining withErrors($validator) (or $validator->errors() in newer versions) with withInput() provides the smoothest user experience, displaying your custom "Harap bagian :attribute di isi." message right next to the relevant input field.

Our network