验证消息未正确显示

问题描述 投票:1回答:2

在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
laravel
2个回答
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.',
     ...

如果一切正常,请检查您的应用程序区域设置,也许您使用的是另一种语言。


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',

    ]);
}
© www.soinside.com 2019 - 2024. All rights reserved.