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?

miken32
  • 42,008
  • 16
  • 111
  • 154
  • Does this answer your question? [Custom Laravel validation messages](https://stackoverflow.com/questions/45007905/custom-laravel-validation-messages) – miken32 Aug 04 '21 at 20:51
  • 1
    no because im using Rules\Password, I know how to customize the default validate but the Rules I don't – IProfessor Man Aug 04 '21 at 20:59
  • I don't think it possible aside from duplicating/extending the Class or overriding the strings via Translations,. – Tobias K. Aug 04 '21 at 21:18

1 Answers1

4

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.

miken32
  • 42,008
  • 16
  • 111
  • 154