4

Using standard notation like "password.required" I can customize an error message for built-in validation rules. But how can I customize error messages for Illuminate\Validation\Rules\Password rules?

$rules = [
    'password' => [
        'required',
        'confirmed',
        Rules\Password::min(8)->letters()->mixedCase()->numbers()->symbols(),
    ],
];
$messages = [
    'password.required'  => 'يجب ادخال كلمة المرور',
    'password.confirmed' => 'كلمة المرور غير متطابقة',
];
$request->validate($rules, $messages);

How to customize the messages for min(), letters(), etc?

3
  • Does this answer your question? Custom Laravel validation messages Commented Aug 4, 2021 at 20:51
  • 1
    no because im using Rules\Password, I know how to customize the default validate but the Rules I don't Commented Aug 4, 2021 at 20:59
  • I don't think it possible aside from duplicating/extending the Class or overriding the strings via Translations,. Commented Aug 4, 2021 at 21:18

2 Answers 2

7

According to this comment in the original pull request, you can't do this in code, and have to use the JSON localization files.

So check the validation class for the default text and then in resources/lang/ar.json add a translation for it, like so:

{
  "The :attribute must contain at least one letter.": ":attribute يجب أن يحتوي على الأقل حرف واحد.",
  "The :attribute must contain at least one uppercase and one lowercase letter.": ":attribute يجب أن يحتوي على الأقل حرف كبير واحد وحرف صغير واحد.",
  "The :attribute must contain at least one number.": ":attribute يجب أن يحتوي على الأقل رقم واحد.",
  "The :attribute must contain at least one symbol.": ":attribute يجب أن يحتوي على الأقل رمز واحد."  
}

The length message uses the standard one found in resources/lang/ar/validation.php:

<?php
return [
  "min" => [
    "string" => "يجب أن يكون طول نص حقل :attribute على الأقل :min حروفٍ/حرفًا.",
  ],
];

Or it can be declared in your code above.

$messages = [
    'password.required'  => 'يجب ادخال كلمة المرور',
    'password.confirmed' => 'كلمة المرور غير متطابقة',
    'password.min' => 'whatever',
];

Note there are packages such as Laravel Lang that can do all these translations for you.

Sign up to request clarification or add additional context in comments.

Comments

0

I had same issue in laravel 11.x.

We can customize error messages in following way:

<Your_Code> 

public function messages()
{
   return [
          'password.required' => 'YOUR_CUSTOM_ERR_MSG',
          'password.confirmed' => 'YOUR_CUSTOM_ERR_MSG',
          'password.mixed' => 'YOUR_CUSTOM_ERR_MSG',
          'password.symbols' => 'YOUR_CUSTOM_ERR_MSG',
          'password.numbers' => 'YOUR_CUSTOM_ERR_MSG',
          'password.uncompromised' => 'YOUR_CUSTOM_ERR_MSG',
          'password.min' => 'YOUR_CUSTOM_ERR_MSG',
     ];
}

<Your_Code>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.