jeudi 1 octobre 2015

Add error to validation errors using formRequest

i'm trying to figure out how to add a error message to the default $error generated by the Illuminate\Support\MessageBag when using validation trough requests.

I've searched on google and the laravel docs and not really found information clarifying this for me.

AuthController

<?php
namespace App\Http\Controllers\Auth;

use Auth;
use App\User;
use App\Http\Requests\LoginFormRequest;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

/*
 * AuthController
 */
class AuthController extends Controller {
    /*
     * Create view
     *
     * @return View
     */
    public function getLogin() {
        return view('auth.login');
    }

    /*
     * Validate Login
     *
     * @return Redirect route
     */
    public function postLogin(LoginFormRequest $request) {
        if (Auth::attempt(array('email' => $request->email, 'password' => $request->password), $request->remember)) {
            return redirect()->intended('home')->with('success-message', 'Succesfully authenticated');
        } else {
            $validator->addMessage('Incorrect email and password combination');
            return redirect('account.login')->withErrors($validator)->withInput();
        }
    }
}

LoginFormRequest

<?php 

namespace App\Http\Requests;

use Response;
use Illuminate\Foundation\Http\FormRequest;

class LoginFormRequest extends FormRequest {

    public function rules() {
        return array(
            'email'    => 'required|email',
            'password' => 'required|between:8,20'
        );
    }

    public function authorize() {
        return true;
    }
}

Hopefully someone has encountered this problem before and can help me!



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1VqhCnM
via IFTTT

Aucun commentaire:

Enregistrer un commentaire