Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel passing errors to views through controllers

I am trying to validate in an action within my controller. once it fails, I send the messages resulting from the validator to the other function in the same controller to pass it to the view. The only problem is I don't see anything displaying in the view and the url changes to a weird format once I click submit and errors occurs.

Controller:

<?php

class MembersController extends BaseController {

    protected $layout = 'layouts.master';


    public function loadRegisterView($input = null)
    {
        if($input == null)
            return View::make('members.register');
        else
            return View::make('members.register', $input);
    }

    public function loadLoginView()
    {
        return View::make('members.login');
    }


    public function register()
    {

        $rules = array(
            'first_name' => 'Required|Min:3|Max:88|Alpha',
            'last_name' => 'Required|Min:3|Max:88|Alpha',
            'password' => 'Required|Min:3|Max:88|Alpha|Confirmed',
            'email' => 'Required|email|unique:users',
        );

        $validator = Validator::make(Input::all(), $rules);


        if ($validator->fails())
        {
            $messages = $validator->messages();
            return Redirect::action('MembersController@loadRegisterView', $messages);
        }else{
            return "Thank you!";
        }

    }
}

Routes:

Route::get('login', array('as' => 'login', 'uses' => 'MembersController@loadLoginView'));

Route::get('signup', array('as' => 'signup', 'uses' => 'MembersController@loadRegisterView'));


Route::get('/', 'MainController@index');


Route::post('processRegistration', array('as' => 'processRegistration', 'uses' => 'MembersController@register'));

in the View:

@if ( $errors->count() > 0 )
      <p>The following errors have occurred:</p>

      <ul>
        @foreach( $errors->all() as $message )
          <li>{{ $message }}</li>
        @endforeach
      </ul>
    @endif

and the URL looks like:

http://localhost:8888/store/index.php/signup?%00%2A%00messages%5Bfirst_name%5D%5B0%5D=The+first+name+field+is+required.&%00%2A%00messages%5Blast_name%5D%5B0%5D=The+last+name+field+is+required.&%00%2A%00messages%5Bpassword%5D%5B0%5D=The+password+field+is+required.&%00%2A%00messages%5Bemail%5D%5B0%5D=The+email+field+is+required.&%00%2A%00format=%3Amessage

Thanks in advance

like image 460
spacemonkey Avatar asked Feb 13 '23 05:02

spacemonkey


1 Answers

You pass variable directly to the route, wile you should do:

    if ($validator->fails())
    {
        $messages = $validator->messages();
        return Redirect::action('MembersController@loadRegisterView')->withErrors($messages);
    }else{
        return "Thank you!";
    }
like image 82
Jarek Tkaczyk Avatar answered Feb 15 '23 20:02

Jarek Tkaczyk