5

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

1 Answer 1

9

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!";
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.