【问题标题】:Laravel - Validation Message not showing on bladeLaravel - 验证消息未显示在刀片​​上
【发布时间】:2020-07-09 09:19:15
【问题描述】:

我无法显示验证错误消息。我尝试了其他类似的问题,但对我不起作用。请帮帮我。

这是我的 Controller.blade.php

    public function store(Request $request)
    {
        $this->validate($request,[
            'surname'=>['required', 'string', 'max:255'],
            'firstname'=>['required', 'string', 'max:255'],
            'birthday'=>'required',
            'pofbirth'=>['required', 'string', 'max:255'],
            'gender'=>['required', 'numeric'],
            'address'=>['required', 'string', 'max:255'],
            'city'=>['required', 'string', 'max:255'],
            'district'=>['required', 'string', 'max:255'],
            'cap'=>['required', 'string', 'max:255'],
            'cstatus'=>['required', 'string', 'max:255'],
            'conjugated'=>['required', 'string', 'max:255'],
            'cellphone'=>['required', 'numeric'],
            'email'=>['required', 'string', 'email', 'max:255'],
            ]);

这是我的 message.blade.php

@if (count($errors) > 0)
  @foreach ($errors->all() as $error)
    <p class="alert alert-danger">{{ $error }}</p>
  @endforeach
@endif

@if (session()->has('message'))

    <p class="alert alert-success">{{ session('message') }}</p>


@endif

这是我的 blade.php

    @include('includes.navbar')
    
    <div class="col" style="background-color: #ffffff;">
        @include('includes.message')
        <form  method="POST" action="{{ route('app.store')}}" enctype="multipart/form-data">
            {{ csrf_field() }}
        <div class="container shadow" style="background-color: rgb(248,249,250);">
            <div class="row">
                <div class="col">
...
...
...

非常感谢!

【问题讨论】:

    标签: laravel validation


    【解决方案1】:

    在您的方法中,您没有任何名为 message 的会话密钥,但在您的验证中没有任何名为 message 的密钥:

    @if (session()->has('message'))
        <p class="alert alert-success">{{ session('message') }}</p>
    @endif
    

    它将是firstnamesurnamegenderage 等(您在验证中使用的密钥)。
    像这样:

    @if($errors->has('firstname'))
        <p class="alert alert-success">{{ $errors->first('firstname') }}</p>
    @endif
    

    获取所有错误信息:

    @if ($errors->any())
       @foreach ($errors->all() as $error)
          <div>{{$error}}</div>
       @endforeach
    @endif
    

    以这种方式更改您的验证:

    $validatedData = $request->validate([
            'surname' => 'required|string|max:255',
            'firstname' => 'required|string|max:255',
            'birthday' => 'required',
            'pofbirth' => 'required|string|max:255',
            'gender' => 'required|numeric',
            'address' => 'required|string|max:255',
            'city' => 'required|string|max:255',
            'district' => 'required|string|max:255',
            'cap' => 'required|string|max:255',
            'cstatus' => 'required|string|max:255',
            'conjugated' => 'required|string|max:255',
            'cellphone' => 'required|numeric',
            'email' => 'required|string|email|max:255',
        ]);
    

    【讨论】:

    猜你喜欢
    • 2020-12-19
    • 2020-02-05
    • 2016-09-12
    • 2020-09-24
    • 2017-01-06
    • 2018-11-05
    • 2018-12-12
    • 1970-01-01
    • 2016-03-29
    相关资源
    最近更新 更多