former icon indicating copy to clipboard operation
former copied to clipboard

Validation Errors and Input::old() is not retrievable when re-routing

Open rizwhn opened this issue 13 years ago • 1 comments

To register a user, I use a form that submits data to a controller, which uses a validate() function in a Model to validate data and pass the validation object back.

views/signup.blade.php

@if($errors->has())

            We encountered the following errors:

            <ul>
            @foreach($errors->all() as $message)

                <li>{{ $message }}</li>

            @endforeach
            </ul>

            @endif
            {{ Form::open(array('route' => 'user.register')) }}
                <!-- Name field -->
                <p>{{ Form::label('fname', 'Name') }}</p>
                <p>{{ Form::text('fname', Input::old('fname'), array('placeholder' => 'First Name')) }}
                    {{ Form::text('lname', Input::old('lname'), array('placeholder' => 'Last Name')) }}</p>
                {{ $errors->first('email') }}
                <!-- username field -->
                <p>{{ Form::label('email', 'Email') }}</p>
                <p>{{ Form::email('email', '', array('placeholder' => 'Email')) }}</p>

                <!-- password field -->
                <p>{{ Form::label('password', 'Password') }}</p>
                <p>{{ Form::password('password') }}</p>

                <!-- password field -->
                <p>{{ Form::label('password_confirmation', 'Verify Password') }}</p>
                <p>{{ Form::password('password_confirmation') }}</p>

                <!-- submit button -->
                <p>{{ Form::submit('Register') }}</p>

            {{ Form::close() }}

controllers/UsersController.php

public function store() {

    $validator = User::validate();

    if ( $validator->fails() ) {
        return Redirect::route('user.register')->withErrors($validator)->withInput();
    }
}

models/User.php

public static $rules = array(
    'fname'  => 'required|max:100',
    'lname'  => 'required|max:100',
    'email'   => 'required|unique:users,email',
    'password' => 'required|confirmed',
    'gender' => 'required|in:male,female'
);
public static function validate($input = null) {
    if (is_null($input)) {
        $input = Input::all();
    }
    $v = Validator::make($input, static::$rules);
}

The problems is that when I use, Redirect::route('user.register')->withErrors($validator)->withInput(); I cannot retrieve Session data in my blade view. However, changing this to Redirect::to('register')->withErrors($validator)->withInput(); works.

rizwhn avatar Sep 05 '13 04:09 rizwhn

Ouch, wrong repo

rizwhn avatar Sep 05 '13 04:09 rizwhn