显示验证消息 Laravel 5.4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45558656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:28:02  来源:igfitidea点击:

Display validation message Laravel 5.4

phplaravellaravel-5

提问by Manh Nguyen

I have a validate like this :

我有一个这样的验证:

TitleRequest.php:

标题请求.php:

public function rules()
{
    return [
        'title_name_report' => 'required|min:2',
        'develop_code' => 'required',
    ];
}

public function messages()
{
    return [
        'title_name_report.required',
        'title_name_report.min',
        'develop_code.required'
    ];
}

Lang/vi/validation.php.

lang/vi/validation.php。

'title_name_report.required'=>"Please enter, :attribute !",
'title_name_report.min'=>"Min length is :attribute",
'develop_code.required'=>"Please enter :attribute !",

'custom' => [
    'attribute-name' => [
        'rule-name' => 'custom-message',
    ],
],

'attributes' => [],

views/title.blade.php

意见/title.blade.php

<input type="text" class="form-control" id="title_name_report" name="title_name_report" 
        value="{{old('title_name_report')}}">
    @if($errors->has('title_name_report'))
        <div class="error-text">
            {{$errors->first('title_name_report')}}
        </div>
    @endif

I have 2 question :

我有两个问题:

1.How can I display exactly errors message, I have 2 rules but it always display the first rule's message. When I try enter 1 character it still display required message.

1.如何准确显示错误消息,我有 2 个规则,但它始终显示第一个规则的消息。当我尝试输入 1 个字符时,它仍然显示所需的消息。

2.How can I use the langs file so I dont need put message into request files.

2.如何使用 langs 文件,以便我不需要将消息放入请求文件中。

Please help me, thank so much !

请帮帮我,非常感谢!

回答by MisaGH

You can show all validation errors with this :

您可以使用此显示所有验证错误:

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

Put custom validation messages in resources/lang/xx/validation.phpfile in this format :

resources/lang/xx/validation.php以这种格式将自定义验证消息放入文件中:

'custom' => [
    'email' => [
        'required' => 'We need to know your e-mail address!',
    ],
],

More Info : https://laravel.com/docs/5.4/validation#custom-error-messages

更多信息:https: //laravel.com/docs/5.4/validation#custom-error-messages

回答by Romnick Susa

  1. How can I display exactly errors message, I have 2 rules but it always display the first rule's message. When I try enter 1 character it still display required message.
  1. 如何准确显示错误消息,我有 2 个规则,但它始终显示第一个规则的消息。当我尝试输入 1 个字符时,它仍然显示所需的消息。

Laravel will only validate your input in order. It will not return an error message of the next rules until it didn't pass the first one.

Laravel 只会按顺序验证您的输入。在没有通过第一个规则之前,它不会返回下一个规则的错误消息。

  1. How can I use the langs file so I dont need put message into request files.
  1. 如何使用 langs 文件,以便我不需要将消息放入请求文件中。

You can find to localization details here. It is under

/resources/lang/en/validation.php

您可以在此处找到本地化详细信息。它在

/resources/lang/en/validation.php

If you want to change the validation message tru Request file, You should do it like this

如果你想改变验证消息TRU请求文件,你应该这样做像这样

public function messages()
{
    return [
        'title_name_report.required' => "Title is required.",
        'title_name_report.min'  => "Title should not less than 2.",
        'develop_code.required'  => "Code is required.",
    ];
}