How to change the field name in validation Laravel 8?

We can customize validation rules in the attributes method. This enables us to override default field names and create more user-friendly names as per our choice for validation error messages. We can simply change field names for validation in the Laravel controller where we are adding the validation rules. Consider the code snippet below.

  $request->validate([
            'email' => 'required|email|unique:users,email',
            // Other validation rules...
        ], [
            'email.required' => 'The :attribute field is required.',
            'email.unique' => 'The :attribute is already taken.',
        ])->attributes([
            'email' => 'Email Address',
            // Add more custom field names as needed...
        ]);

So, when you have to display error messages related to email address. You can pass the attributes method as shown above. Here, you can add any custom field names to show in place of email when showing up email validation messages.

Similarly, we can customize other field names like or the password and other fields. In the attributes method, we can add. as many fields as possible for a single field name or different field names.

Leave a Comment