Laravel language on Auth validation error

£可爱£侵袭症+ 提交于 2019-12-13 07:07:10

问题


I would like to use the laravel default functionality to show the error, but in another language. I don't need a "nice name", but a translation for the **:attribute value in lang files. Right now if I just use:

<input type="text" placeholder="{{ trans('generic.phone') }}" name="phone" value="{{ old('phone') }}">
@if ($errors->has('phone'))
<span class="help-block">
    <strong>{{ $errors->first('phone') }}</strong>
</span>
@endif

it works perfectly, because the :attribute takes the field phone, and in the validation language I have:

'required' => 'The :attribute field is required.',

But how to manage phone field with a language file?

Do I must have to write a custom error for each field? Please tell me I don't.

The following function is just a wrong example to let you understand what I'm trying to do

@if ($errors->has('phone'))
    <strong>{{ $errors->first(trans('generic.phone')) }}</strong>
@endif

回答1:


Really simple! Just add attributes in the language file! In my case, for lang/it/validation.php

I just set:

'attributes' => [
    'phone' => 'telefono',
],

while all remain the same for the sentences

'required' => 'Il campo :attribute &grave; obbligatorio.',


来源:https://stackoverflow.com/questions/41897327/laravel-language-on-auth-validation-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!