Home > Software design >  Errors are not displaying in the view file
Errors are not displaying in the view file

Time:09-22

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..

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

<!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 >
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif
  <div >
    <img src="img_avatar2.png" alt="Avatar" >
  </div>

  <div >
    <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  style="background-color:#f1f1f1">
    <button type="button" >Cancel</button>
    <span >Forgot <a href="#">password?</a></span>
  </div>
</form>

</body>
</html>

CodePudding user response:

Use withErrors() instead of 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()


FYK

  1. has() in validations
  2. Manually Creating Validators

CodePudding user response:

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 >
    {{ session('errors') }}
</div>
@endif
  • Related