How to set laravel custom validation message with laravel rules

How to set laravel custom validation message with laravel rules

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

Stefan Bogdanescu

Founder & Senior Architect

Customizing Laravel Validation Messages Beyond Default Rules

You have a working validation pipeline in your save controller method and a static rules() helper on your Venue model. When the form submits, Laravel throws an exception if the data doesn't match your rules, and you want to control how that exception is rendered instead of accepting the default English messages. Laravel gives you three main places to define those messages: inline in the request validation array, globally in a service provider or configuration file, and locally on specific fields using the attributes key.

Inline Validation Messages

The quickest change you can make is to pass an extra argument to validate() that maps field names to custom error messages. In your controller, update the call like this:

public function save(Request $request) {
    try {
        $this->validate(
            $request,
            Venue::rules(),
            [
                'name' => 'required :attribute is mandatory',
                'logo' => 'required :attribute must be provided',
                'status' => 'required :attribute must be one of the allowed values',
                'venue_type_id' => 'required :attribute cannot be zero',
                'client_id' => 'required :attribute cannot be zero',
            ]
        );

        $venue = Venue::saveOrUpdate($request);

        if ($venue !== false) {
            if ($request->get('continue', false)) {
                return redirect()->route('admin.venue.edit', ['id' => $venue->id])->with('success', trans('admin.venue.save_success'));
            } else {
                return redirect()->route('admin.venue.index')->with('success', trans('admin.venue.save_success'));
            }
        } else {
            return back()->with('error', "Unable to save venue")->withInput();
        }
    } catch (\Exception $ex) {
        // In production, consider logging the exception before returning an error view
        return back()->with('error', "Unable to save venue")->withInput();
    }
}

This array uses Laravel's standard message template syntax. The :attribute placeholder is replaced with the field label from your form, and you can also use :input for the raw submitted value. For example:

'name' => 'The :attribute must contain at least 3 characters',

becomes:

The name must contain at least 3 characters

Global Validation Messages

If you need consistent messages across many controllers or want to decouple them from controller logic, define a global validation language file. Laravel expects files in lang/<locale>/validation.php. For an English app, create or update:

// resources/lang/en/validation.php
return [
    'required' => ':attribute is required',
    'string'   => ':attribute must be a string',
    'max'      => ':attribute may not exceed :max characters',
    // You can also override specific rules like 'in' or 'not_in' if desired
];

To customize messages for your in and not_in rules specifically, extend the base definitions:

return [
    'required' => ':attribute is required',
    'in'       => ':attribute must be one of: :values',
    'not_in'   => ':attribute cannot be any of: :values',
];

Global files are useful when your application uses multiple locales. You can then create lang/<locale>/validation.php for other languages and switch them at runtime or via the localization system.

Model-Level Customization

Your Venue::rules() method currently returns an array of rules. Laravel also supports attaching custom messages directly to the rules array using the ->messages() syntax before passing it to validate(). However, since you're calling a static method that returns only rules, you'd need to either:

  1. Modify Venue::rules() to return both rules and messages as an associative array keyed by field, or
  2. Keep it simple by defining messages inline in the controller (as shown above).

For instance, you could change your model's static method to include messages:

public static function rules($id = '') {
    $messages = [
        'name' => 'The venue name is required',
        'logo' => 'A logo image must be provided',
        'status' => 'Status is required and must be one of the allowed values',
        'venue_type_id' => 'Venue type ID is required and cannot be zero',
        'client_id' => 'Client ID is required and cannot be zero',
    ];

    $rules = [
        'name' => ['required', 'string', 'max:255'],
        'logo' => ['required', 'image'], // Adjust based on your actual logo rule
        'status' => ['required', 'string', 'in:' . implode(',', Venue::STATUSES)],
        'venue_type_id' => ['required', 'string', 'not_in:0'],
        'client_id' => ['required', 'string', 'not_in:0'],
    ];

    return compact('rules', 'messages');
}

Then in the controller, extract and pass only the rules while keeping messages separate if needed.

Trade-offs and Common Pitfalls

  • Inline vs Global: Inline messages are easy to tweak per request but harder to maintain at scale. Global files promote consistency but require careful versioning when your app supports multiple locales.
  • Placeholder Consistency: Always use :attribute or :input correctly; missing colons break the substitution and show raw templates in views.
  • Rule Complexity: If you mix dynamic values (like implode for in:), ensure the resulting string is safe and readable in error messages.

By leveraging either inline validation messages, global language files, or model-level message arrays, you gain full control over how Laravel presents validation errors without relying on default translations. For more guidance on internationalization and validation configuration patterns, see the LaravelCompany resource at https://laravelcompany.com for additional reference material.

Our network