简体   繁体   English

错误未显示在视图文件中

[英]Errors are not displaying in the view file

I am using laravel for developing web application,i have one login controller if it fails i am sending invalid credentials message to the blade file but it's not showing my error message can anyone correct me..我正在使用 laravel 来开发 web 应用程序,我有一个登录 controller 如果它失败,我将向任何人发送无效凭据消息给刀片文件,但它没有显示我的错误消息。

controller.php controller.php

public function login(UserRequest $request){
        $this->repository->pushCriteria(new WhereCriteria('email',$request->email));
        $this->repository->pushCriteria(new WhereCriteria('password',$this->binaryPassword($request->password)));
        $data = $this->repository->get();
        if(count($data) < 1){
           return  redirect()->back()->with('errors','Invalid credentials');
        }
        redirect('/dashboard');
    }

sign-in.blade.php登录.blade.php

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

<h2>Login Form</h2>

<form action="<?php echo url('login-chk'); ?>" method="POST">
@csrf
@if (count($errors) > 0)
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif
  <div class="imgcontainer">
    <img src="img_avatar2.png" alt="Avatar" class="avatar">
  </div>

  <div class="container">
    <label for="uname"><b>Username</b></label>
    <input type="text" placeholder="Enter Username" name="email" required>

    <label for="psw"><b>Password</b></label>
    <input type="password" placeholder="Enter Password" name="password" required>

    <button type="submit">Login</button>
    <label>
      <input type="checkbox" checked="checked" name="remember"> Remember me
    </label>
  </div>

  <div class="container" style="background-color:#f1f1f1">
    <button type="button" class="cancelbtn">Cancel</button>
    <span class="psw">Forgot <a href="#">password?</a></span>
  </div>
</form>

</body>
</html>

Use withErrors() instead of with使用withErrors()而不是with

return Redirect()->back()->withErrors(['count' => 'Invalid credentials']);

In Blade在刀片

if ($errors->has('count')) {
    echo 'message';
}

If you need to redirect with inputs then use withInput()如果您需要使用输入重定向,请使用withInput()


FYK FYK

  1. has() in validations has()在验证中
  2. Manually Creating Validators 手动创建验证器

Controller Controller

   public function login(UserRequest $request){
    $this->repository->pushCriteria(new WhereCriteria('email',$request->email));
    $this->repository->pushCriteria(new WhereCriteria('password',$this->binaryPassword($request->password)));
    $data = $this->repository->get();
    if(count($data) < 1){
       return  redirect()->back()->with('errors','Invalid credentials');
    }
    redirect('/dashboard');
}

Blade

@if (session('errors'))
<div class="alert alert-success">
    {{ session('errors') }}
</div>
@endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM