验证消息未正确显示

时间:2018-03-01 21:08:15

标签: laravel

在validation.php文件中,错误消息如下:

'after_or_equal'       => 'A :attribute must be a date after or equal to :date.',

但是,当出现此错误时,显示的消息为:

validation.after_or_equal

你知道为什么吗?

在视图中显示消息的代码:

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

存储方法验证:

public function store(Request $request)
{

    $this->validate($request, [

        'startDate' => 'required|date_format:d F Y - H:i',
        'endDate' => 'required|date_format:d F Y - H:i|after_or_equal:startDate',

    ]);
}

包含错误文件的视图:

@extends('layouts')
@section('content')
<div class="container-fluid px-4">

@include('includes.errors')

<form id="edit_admins" method="post" class="clearfix"
  action="{{route('admins.update', ['post_id' => $post->id])}}" enctype="multipart/form-data">
{{csrf_field()}}

....

</form>

@endsection

2 个答案:

答案 0 :(得分:1)

因此,请确认您在何处放置了代码'after_or_equal' => 'A :attribute must be a date after or equal to :date.',,因为如果您放入密码,您将收到此消息validation.after_or_equal。 您需要在 validation.php

上放置此位置
<?php

return [

   /*
   |------------------------------------------------------------------------
   | Validation Language Lines
   |--------------------------------------------------------------------------
   |
   | The following language lines contain the default error messages used by
   | the validator class. Some of these rules have multiple versions such
   | as the size rules. Feel free to tweak each of these messages here.
   |
   */

     'accepted'             => 'The :attribute must be accepted.',
     'active_url'           => 'The :attribute is not a valid URL.',
     'after'                => 'The :attribute must be a date after :date.',
     'after_or_equal'       => 'A :attribute must be a date after or equal to :date.',
     ...

如果一切正常,请检查您的应用区域,也许您使用其他语言。

答案 1 :(得分:0)

问题似乎是date_format验证规则应该包含"所包含的日期格式。

这是更新后的代码段。

public function store(Request $request)
{

    $this->validate($request, [

        'startDate' => 'required|date_format:"d F Y - H:i"',
        'endDate' => 'required|date_format:"d F Y - H:i"|after_or_equal:startDate',

    ]);
}