awaistoor
awaistoor

Reputation: 786

Laravel withError function not working

I was testing this new framework (Laravel) and upto now i'm following those examples which they have put in their documentation. All is going good till i got this problem.

i am calling Redirect::to('\signup')->withErrors($validator->messages()); but its not showing anything but if i just print it like print_r($validator->messages()); it shows me the errors. Please help me and tell me where i am going wrong?

Controller

public function registerUser(){
    $validator = Validator::make(Input::all(),array(
            'username' => 'required|email|unique:user',
            'password' => 'required|min:8',
            'fullname' => 'required'
        )
    );
    if($validator->fails()){

        return Redirect::to('signup')->withInput()->withErrors($validator);
    }else{
        return Redirect::route('user');
    }

}

Route Route::get('/signup',array('as'=>'signup', 'uses'=>'HomeController@signUp'));

View

<?php print_r($errors) ; ?>

Upvotes: 0

Views: 7528

Answers (2)

awaistoor
awaistoor

Reputation: 786

The problem was that I changed the secure option to true in session file (in config folder) and was testing it on local server without https. That is why my errors were not showing in view.

Upvotes: 0

Kevin G.
Kevin G.

Reputation: 604

Here's the relevant information from the Laravel website:

Route::post('register', function()
{
    $rules = array(...);

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

    if ($validator->fails())
    {
        return Redirect::to('register')->withErrors($validator);
    }
});

Which you can reference from Blade like this...

<?php echo $errors->first('email'); ?>

Notice how $errors is an object? You call print_r($errors) which may not produce very useful output.

Try this...

<?php
    var_dump($errors->first('username'));
    var_dump($errors->first('password'));
    var_dump($errors->first('email'));
?>

Also: Your unique:user validation should probably be unique:user,username. Use the database table name and column name, not the model values.

Note: If you are testing this in a browser, you may want to look into using Clockwork, which allows you to see Session information from the browser without the print_r/var_dump calls.

Upvotes: 3

Related Questions