【问题标题】:Custom Validation message on laravellaravel 上的自定义验证消息
【发布时间】:2020-10-13 09:51:56
【问题描述】:

我想在 laravel 上自定义这个验证消息,我不知道该怎么做。

我用这个来显示错误

@if ($errors->any())
    <div class="alert alert-danger">
        <strong>Whoops!</strong> There were some problems with your input.<br><br>
        <ul>
            @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

我在下面的模型上使用我的验证

public static $validation = [
        'schedule_doctor_id' => 'required',
        'nama_lengkap'       => 'required|string',
        'email'              => 'required',
        'no_tlp'             => 'required',
        'tanggal'            => 'required|date_format:d-m-Y'
    ];

【问题讨论】:

标签: laravel validation


【解决方案1】:

默认情况下,你可以发现所有的消息验证都是这样定义在resources\lang\en\validation.php中的

'accepted'             => 'The :attribute must be accepted.',
'active_url'           => 'The :attribute is not a valid URL.',
'alpha'                => 'The :attribute may only contain letters.',
...

with :attribute 自动替换为输入的名称,您可以在此文件中编辑消息。但在我看来,你不应该那样做。

另一种方式。

您创建表单请求验证php artisan make:request UserRequest

/**


* Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
    ];
}

/**
 * Get the error messages for the defined validation rules.
 *
 * @return array
 */
public function messages()
{
    return [
        'title.required' => 'A title is required', /** You can custom message here */
        'body.required' => 'A message is required',
    ];
}

【讨论】:

    【解决方案2】:

    Laravel 框架的验证功能默认支持自定义消息。

    阅读此链接; https://laravel.com/docs/8.x/validation#custom-error-messages

    【讨论】:

      猜你喜欢
      • 2014-05-31
      • 2017-06-28
      • 2017-12-13
      • 2020-07-18
      • 1970-01-01
      • 2016-01-29
      • 2015-07-13
      • 2019-11-01
      • 1970-01-01
      相关资源
      最近更新 更多