无法在Laravel中显示验证错误信息

huangapple go评论71阅读模式
英文:

Unable to display validation errors in Laravel

问题

我尝试显示使用验证创建的表单的错误,但当出现错误时,显示上没有任何内容。

我尝试了@dump(Session::has('status')) @dump($errors->any()),但每次在我的视图中这两个值都保持为false。

这是我的登录函数。

public function login(Request $request)
{
    $credentials = $request->validate([
        'email' => 'email|max:90|required',
        'password' => 'required|min:6',
    ]);

    if (Auth::attempt($credentials, true)) {
        $accessToken = auth()->user()->createToken('authToken')->plainTextToken;
        return redirect('/home')->withCookie('auth_token', $accessToken);
    } else {
        $notification = 'L\'email ou le mot de passe est incorrecte';
        return back()->with('status', $notification);
    }
}

我的表单

<form action="{{ url('/authentification') }}" class="request-form ftco-animate bg-primary" method="POST">@csrf
   <h2>Connexion</h2>

    @if ($errors->any())
        <div class="alert alert-danger">
                <ul>
                   @foreach ($errors->all() as $error)
                       <li>{{ $error }}</li>
                   @endforeach
                 </ul>
       </div>
       @endif
    @if (Session::has('status'))
       <div class="alert alert-danger">
           {{ Session::get('status') }}
       </div>
    @endif
    <div class="form-group">
     <label for="" class="label">Email</label>
     <input type="text" class="form-control" placeholder="Email" name="email">
    </div>
    <div class="form-group mr-2">
    <label for="" class="label">Mot de passe</label>
       <input type="password" class="form-control" placeholder="**********" name="password">
 </div>
<div class="form-group">
  <input type="submit" value="Connexion" class="btn btn-secondary py-3 px-4">
   <a href="{{ url('/register') }}" class="text-center" style="color: white;">Pas encore de compte ?</a>
</div>
</form>

提前感谢您的帮助。

英文:

I try to display the errors of my form that I create with validation but nothing shows on the display when I have an error

I tried @dump(Session::has(&#39;status&#39;)) @dump($errors-\&gt;any()), but each time the 2 values remain false in my view.

Here is my login function.

    public function login(Request $request)
    {
        $credentials = $request-&gt;validate([
            &#39;email&#39; =&gt; &#39;email|max:90|required&#39;,
            &#39;password&#39; =&gt; &#39;required|min:6&#39;,
        ]);

        if (Auth::attempt($credentials, true)) {
            $accessToken = auth()-&gt;user()-&gt;createToken(&#39;authToken&#39;)-&gt;plainTextToken;
            return redirect(&#39;/home&#39;)-&gt;withCookie(&#39;auth_token&#39;, $accessToken);
        } else {
            $notification = &#39;L\&#39;email ou le mot de passe est incorrecte&#39;;
            return back()-&gt;with(&#39;status&#39;, $notification);
        }
    }

My form

&lt;form action=&quot;{{ url(&#39;/authentification&#39;) }}&quot; class=&quot;request-form ftco-animate bg-primary&quot;method=&quot;POST&quot;&gt; @csrf
       &lt;h2&gt;Connexion&lt;/h2&gt;

        @if ($errors-&gt;any())
            &lt;div class=&quot;alert alert-danger&quot;&gt;
                    &lt;ul&gt;
                       @foreach ($errors-&gt;all() as $error)
                           &lt;li&gt;{{ $error }}&lt;/li&gt;
                       @endforeach
                     &lt;/ul&gt;
           &lt;/div&gt;
           @endif
        @if (Session::has(&#39;status&#39;))
           &lt;div class=&quot;alert alert-danger&quot;&gt;
               {{ Session::get(&#39;status&#39;) }}
           &lt;/div&gt;
        @endif
        &lt;div class=&quot;form-group&quot;&gt;
         &lt;label for=&quot;&quot; class=&quot;label&quot;&gt;Email&lt;/label&gt;
         &lt;input type=&quot;text&quot; class=&quot;form-control&quot; placeholder=&quot;Email&quot; name=&quot;email&quot;&gt;
        &lt;/div&gt;
        &lt;div class=&quot;form-group mr-2&quot;&gt;
        &lt;label for=&quot;&quot; class=&quot;label&quot;&gt;Mot de passe&lt;/label&gt;
           &lt;input type=&quot;password&quot; class=&quot;form-control&quot; placeholder=&quot;**********&quot; name=&quot;password&quot;&gt;
     &lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt;
      &lt;input type=&quot;submit&quot; value=&quot;Connexion&quot; class=&quot;btn btn-secondary py-3 px-4&quot;&gt;
       &lt;a href=&quot;{{ url(&#39;/register&#39;) }}&quot; class=&quot;text-center&quot; style=&quot;color: white;&quot;&gt;Pas encore de compte ?&lt;/a&gt;
&lt;/div&gt;
&lt;/form&gt;

I thank you in advance for your help

答案1

得分: 0

当您使用with()方法从控制器返回状态时,它将变量返回,而不是作为会话键。因此,在Blade文件中:

@if ($status ?? false)
    <div class="alert alert-danger">
        {{ $status }}
    </div>
@endif

在您的控制器中:

return back()->with('status', $notification);

或者,如果您想使用会话:

在控制器中:

return back()->withErrors('status', $notification);

在Blade代码中保持不变。

英文:

When you return status from controller with with() method it returns variable not as session key so in blade file:

@if ($status ?? false)
   &lt;div class=&quot;alert alert-danger&quot;&gt;
       {{ $status }}
   &lt;/div&gt;
@endif

and in your controller

return back()-&gt;with(&#39;status&#39;, $notification);

OR if you want to use session instead :
in controller:

return back()-&gt;withErrors(&#39;status&#39;, $notification);

and in blade code will remain the same.

references: https://laracasts.com/discuss/channels/laravel/return-success-message

答案2

得分: 0

安装 Blade 时,未从 Laravel Breeze 安装 auth-validation-errors-blade.php 文件。

英文:

When installed blade,auth-validation-errors-blade.php file did not installed from laravel breeze

huangapple
  • 本文由 发表于 2023年3月15日 20:11:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定