jeudi 28 mai 2015

Laravel redirect from private method with errors

I have the following code:

public function store(Request $request)
{
        $this->validateData($request->all());

        // store something

        return redirect()->action('controller@index')->withMessage( 'Saved Successfully' );
}

private function validateData($requestParams)
{
    try 
    {
        $validator->validate( $requestParams );
    } 
    catch ( ValidationException $e ) 
    {
        redirect()->action('controller@create')->withInput()->withErrors( $e->get_errors() )->send();
        exit(); // this causes the withErrors to not be there
    }
}

If I remove the exit();, the error messages will appear, but also the store function will be executed (see // store something). I know I can rewrite my code like:

if($this->validateData($request->all()))
{
    // store something

    return redirect()->action('controller@index')->withMessage( 'Saved Successfully' );
}

But I don't want the ugly if statement here. There must be a way to redirect with the flash messages without it.

Thanks in advanced!

Aucun commentaire:

Enregistrer un commentaire