在laravel 4.2中重定向错误

时间:2016-01-07 11:05:41

标签: laravel-4

以下是UserController中的方法

     public function callSignupForm($email) {

            return View::make('Signup')->with('email', $email);

    }

    public function SignuptoDatabase() {

        $input = Input::all();

        $email = Input::get('emailText');


        $name = Input::get('nameText');
        $username = Input::get('usernameText');
        $password = Input::get('pwd');
        $confirmpassword = Input::get('cpwd');
        $Gender = Input::get('Gender');

        $rules = array(
            'email' => 'required|unique:users',
            'name' => 'required',
            'username' => 'required|unique:users',
            'password' => 'required|min:8',
            'confirmpassword' => 'same:password',
            'Gender' => 'required|in:Male,Female'
        );

        $validator = Validator::make($input, $rules);

        if ($validator->passes()) {

            $user = new User;
            $user->name = $name;
            $user->username = $username;
            $user->password = password;
            $user->email = email;
            $user->save();
        }
return Redirect::action('UserController@callSignupForm')->withErrors($validator)->with('email', $email);
    }

Signup.blade.php

     {{Form::open(array('url'=>'signuptoDatabase'))}}
             @foreach ($errors->all() as $error)

                            <p class="errorMessage">{{ $error }}</p>

                            @endforeach
            <div class="form-group">
                <label>Email</label> 
                <input type="text" class="form-control" name="emailText" value='{{$email}}'/>
            </div>
            <div class="form-group">
                <label>Name</label>
                <input type="text" class="form-control" name="nameText">
            </div>
            <div class="form-group">
                <label>Username</label>
                <input type="text" class="form-control" id="usernameText">
            </div>
            <div class="form-group">
                <label>Password</label>
                <input type="password" class="form-control" id="pwd" autocomplete="off"/>
            </div>
            <div class="form-group">
                <label>ConfirmPassword</label>
                <input type="password" class="form-control" id="cpwd" autocomplete="off"/>
            </div>
            <div class="form-group">
                <label>Gender</label>
                <label><input type="radio" id="Gender" name="Male" checked="true">Male</label>
                <label><input type="radio" id="Gender" name="Female">Female</label>
            </div>

               </div>  
            <div class="panel-footer clearfix">
                <div class="pull-right">
                <button type="submit" Id="registersubmit" class="btn btn-success btn-lg" >Submit</button>
            </div>
</div>            
        {{Form::close()}}

在UserController中调用SignuptoDatabase函数之后。如果有任何错误。我尝试重定向到包含错误和输入的同一页面。

我试过

return Redirect::action('UserController@callSignupForm')->withInput()->withErrors($validator);

Redirect::back()

并且通过路由,我的路线看起来像

Route::post('callsignupform','UserController@callSignupForm');

什么都行不通..任何人都建议我解决方案......

2 个答案:

答案 0 :(得分:0)

像这样使用return语句

return Redirect::back()->withErrors($validator->messages())->withInput();

答案 1 :(得分:0)

你也可以像这样使用return stament

return Redirect::back()->withInput()->with('message','Woops! There were some problems with your input.');

并在视图文件中添加以下代码以显示错误

<?php echo Session::get("message");?>