NARGIS PARWEEN
NARGIS PARWEEN

Reputation: 1596

Laravel Custom Validation for multi-language application

I am using Laravel Standard Validation, but I want to customize it because I am using multi language in my application, so I have to customize the message.

      $rule_validation = [
        'phone' => 'required|max:20|min:6|regex:/^[0-9]+$/',
        'agree_promo_code' => request('promo_code') ? 'accepted' : '',
        'terms_of_services' => 'accepted',
        'aware' => 'accepted'
        ];

Now I want to write customize validation message for agree_promo_code

I know to write message for phone, but having doubt in agree_promo_code, terms_of_services and aware. Can anyone help me out to resolve this issue

But please keep in mind about the language. Thank you

Upvotes: 0

Views: 1722

Answers (2)

Mr Singh
Mr Singh

Reputation: 112

As you set your Language files for respective language you need to create validation.php file under respective language folder and add custom message for each field for each type of validation applied

for example if you have name field and validation check is required so in your language folder's validation file you will write as below

    <?php
return [
  'custom' => [
    'name' => [
      'required' => 'O campo nome é obrigatório.'
     ]
    ];

rest all language check will be handled by Laravel itself while displaying validation error messages as per your locale language selected for session

Upvotes: 1

Himanshu Raval
Himanshu Raval

Reputation: 811

You can pass second array to your validate function like

 $this->validate($request,[
            'phone' => 'required|max:20|min:6|regex:/^[0-9]+$/',
            'agree_promo_code' => request('promo_code') ? 'accepted' : '',
             'terms_of_services' => 'accepted',
             'aware' => 'accepted'
        ],[
            'phone.required'  => trans('validation.phone'),
            'agree_promo_code.accepted'  => trans('validation.agree_promo_code'),
            'terms_of_services.accepted'  => trans('validation.terms_of_services'),
              'aware.accepted'  => trans('validation.aware'),
        ]);

and inside your resources/lang/{lang}/validation.php file ({lang} is your language directory).

you can do something like

return [
      'phone' => 'Phone validation message',
      'agree_promo_code' => 'agree_promo_code validation message',
      'terms_of_services' => 'terms_of_services validation message',
];

So it will set your messages according to respective language.

Upvotes: 2

Related Questions