vendredi 30 décembre 2016

Laravel 5.1 login register display custom error messages

I was start working with basic Auth in Laravel, described here: http://ift.tt/2hDeA2h

Everything works properly, when I enter right credentials I will authorised, and redirected, thats great but, when I enter bad credentials, nothing happens, just refresh the form...

I need to display custom error messages like "Bad credentials", "Please fill all inputs" for register ... "Email already registred" etc.

But I do not know, how can I do that.

Which method, or which file to edit to do that.

My login form looks simple from docs:

<form method="POST" action="login">
    {!! csrf_field() !!}

    <div>
        Email
        <input type="email" name="email" value="">
    </div>

    <div>
        Password
        <input type="password" name="password" id="password">
    </div>

    <div>
        <input type="checkbox" name="remember"> Remember Me
    </div>

    <div>
        <button type="submit">Login</button>
    </div>
</form>

Register form too:

<form method="POST" action="register">
    {!! csrf_field() !!}

    <div>
        Email
        <input type="email" name="email" value="">
    </div>

    <div>
        Password
        <input type="password" name="password">
    </div>

    <div>
        Confirm Password
        <input type="password" name="password_confirmation">
    </div>

    <div>
        <button type="submit">Register</button>
    </div>
</form>

And AuthController like this:

 use AuthenticatesAndRegistersUsers, ThrottlesLogins;
    private $redirectTo = '/testing';
    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }
}

Thank you very much, have a great time!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire