Redirect :: back () call of another function does not work. Laravel 4

advertisements

I'm new in Laravel and some times it's hard to undestand waht's happening.

I have a code for validate a form. If I use it in a function it works right, but dosn't work the Redirect:back() if a call it from a other function. I want to reuse the code for all forms, but can't refactor.

This is the code (not working, does't redirect, continue executing code if validation fails)

private function formValidation($data ,$rules ,$error_name = 'user', $message = 'Hay errores en el formulario')
{
    $validator = Validator::make($data, $rules);

    if ($validator->fails())
    {
        Session::Flash('message' , $message);
        return Redirect::back()->withErrors($validator,$error_name)->withInput();
    }
    else
        return $validator;
}

public function loginPost()
{
    $this->formValidation(Input::all(),User::$rules_login);
    ..........

This code works fine, it redirects if validation fails:

public function loginPost()
{
    $validator = Validator::make(Input::all(),User::$rules_login);

    if ($validator->fails())
    {
        Session::Flash('message' , 'Hay errores en el formulario');
        return Redirect::back()->withErrors($validator,'user')->withInput();
    }
    .............

Can anybody help me plese? Really I want to call it from another class, but after see that does not work I call fron a private function.

I try calling Redirect:to('login', $data, $rules...) and with Redirect::route too, still not working.

Tanks.


This works, so I can refactor ;)

private function formValidation($data ,$rules , $message = 'Hay errores en el formulario')
{
    $validator = Validator::make($data, $rules);

    if ($validator->fails()) {
        MyHelpers::sendSessionFlashMessages(array('message' => $message)); // Send a Session::flash with css information
        return Redirect::back()->withErrors($validator, 'user')->withInput();
    }

    return $validator;
}

public function loginPost()
{
    if (get_class($response = $this->formValidation(Input::all(),User::$rules_login)){ === 'Illuminate\Http\RedirectResponse')
        return $response;
    }
...................... rest of the function