Laravel 5.5 The email and password field is required

Viewed 7641

I am new and installed Laravel 5.5 and trying to work on login functionality. I am using custom fields to login; user_name and user_password. When I submit I am getting

  • The email field is required.
  • The password field is required.

but my fields are user_name and user_password in the user table

Login controller

namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Session;

class LoginController extends Controller
{


use AuthenticatesUsers;


protected $redirectAfterLogout = '/';

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
}

/**
 * Logout, Clear Session, and Return.
 *
 * @return void
 */
public function logout()
{
    $user = Auth::user();
    Log::info('User Logged Out. ', [$user]);
    Auth::logout();
    Session::flush();

    return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
}

protected function getCredentials(Request $request){
    return $request->only('user_name', 'user_password');
}


}

View

@extends('layouts.app')

@section('content')
<div class="container">
<div>
  <a class="hiddenanchor" id="signup"></a>
  <a class="hiddenanchor" id="signin"></a>
  <div class="login_wrapper">
    <div class="animate form login_form">
      <section class="login_content">
        <form class="form-horizontal" role="form" method="POST" action="{{ route('login') }}">
          {{ csrf_field() }}
          <h1>Login Form</h1>
          <div>
            <input id="email" type="email" class="form-control" name="user_name" value="{{ old('user_name') }}" required autofocus>
          </div>
          <div>
            <input id="password" type="password" class="form-control" name="user_password" required>
          </div>
          <div>
            <button type="submit" name="login" class="btn btn-default submit">Log in</button>
            <a class="reset_pass" href="#">Lost your password?</a>
          </div>

          <div class="clearfix"></div>
          <div class="separator"></div>
        </form>
      </section>
    </div>
 </div>
</div>
2 Answers

You are using the trait AuthenticatesUsers and it is responsible for validating the data after the user submitted the form. If you take a look at this file, you will see this method:

/**
 * Validate the user login request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return void
 */
protected function validateLogin(Request $request)
{
    $this->validate($request, [
        $this->username() => 'required|string',
        'password' => 'required|string',
    ]);
}

More specifically, as you can see, this method is in charge for calling the validator on the fields name "password" and by the method username(). If you only wanted to custom the username field, you could overwrite the username() method in LoginController class:

public function username()
{
    return 'user_name';
}

But since you want to custom both the username field name and the password field name, I suggest that you overwrite the validateLogin() method in the LoginController class:

protected function validateLogin(Request $request)
{
    $this->validate($request, [
        'user_name' => 'required|string',
        'user_password' => 'required|string',
    ]);
}

Hope that helps.

You must go to app/User.php and change

protected $fillable = [
    'name', 'email', 'password',
];

to

protected $fillable = [
    'name', 'user_email', 'user_password',
];

It will solve your problem. But i must suggest to you change your view form field to email, password instead of user_name, user_password

Related