【问题标题】:validation message is not appearing properly验证消息未正确显示
【发布时间】:2018-03-01 21:08:14
【问题描述】:

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


【解决方案1】:

所以确认你把你的代码'after_or_equal' =&gt; '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.',
     ...

如果一切正常,请检查您的应用语言环境,也许您使用的是另一种语言。

【讨论】:

  • 谢谢,这似乎是因为应用程序区域设置,所以如果我们更改应用程序区域设置,您知道我们该怎么做才能获得正确的验证消息吗?
  • 很好,所以你可以使用这个包github.com/caouecs/Laravel-lang,在那里你可以找到几乎所有的语言。要更改语言验证,请在 `application\resources\lang\{language}` 中手动创建一个带有所需语言的 文件夹,并在其中创建 validation.php文件并放置验证文本。
【解决方案2】:

问题似乎是date_format 验证规则应具有由" 包围的日期格式。

这是更新后的 sn-p。

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

    ]);
}

【讨论】:

  • 谢谢,但消息显示相同的“validation.after_or_equal”。
  • 该死。如果您在 startDate 上验证失败,是否会发生这种情况?
  • 是的,消息是一样的,但“validation.date_format”。
猜你喜欢
  • 2013-11-14
  • 2021-10-17
  • 1970-01-01
  • 1970-01-01
  • 2013-07-16
  • 1970-01-01
  • 2016-03-29
  • 2020-12-26
  • 1970-01-01
相关资源
最近更新 更多