G
G
GAS-ART2022-03-14 09:40:42
Laravel
GAS-ART, 2022-03-14 09:40:42

Is it possible to use the Locale facade in request?

I validate the data from the form using request:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ContactRequest extends FormRequest
{
    public function rules()
    {
        return [
         'name' => 'required||min:2|max:80|regex:/^[^0-9]+$/',
         'service' => 'required',
         'email' => 'required|email',
         'filename' => 'max:25000',
         'phone' => 'required|regex:/^((\s*)?(\+)?)([- _():=+]?\d[- _():=+]?){10,12}(\s*)?$/'
        ];
    }

    public function messages(){
        return[
           'name.required' => "Не заполнено поле \"Имя\"",
           'name.min' => "Поле \"Имя\" должно содержать 2 или больше символов",
           'name.max' => "Поле \"Имя\" должно содержать не больше 80 символов",
           'name.regex' => "Поле \"Имя\" не должно содержать цифр",
           'service.required' => "Пожалуйста выберете тип услуги из списка",
           'email.required' => "Не заполнено поле \"email\"",
           'email.email' => "Указан не корректный email адрес",
           'filename.max' => "Максимально допустимый размер файла 25 мегабайт",
           'phone.regex' => "Не верный формат номера телефона",
           'phone.required' => "Не заполнено поле \"Номер телефона\""
        ];
     }
}

Tell me, can I connect the Locale facade here, determine the current localization and substitute the option from the localization files instead of the fixed array values? like
'name.required' => @lang(request.namerequired):
In blade templates, this is done with the @lang directive or the __() helper function. But what is the right way to do it in the request file?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey delphinpro, 2022-03-14
@delphinpro

Use the same __() function
And for some of the standard validations, there are already texts in lang/en/validation.php

J
jazzus, 2022-03-14
@jazzus

helper trans

V
Vyacheslav Plisko, 2022-03-14
@AmdY

Does localization out of the box not work for validation errors? Everything should be, take a look at the documentation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question