Laravel验证投掷路线错误

时间:2014-06-30 16:28:13

标签: validation laravel laravel-4

我正在尝试对控制器中的表单字段进行基本验证检查。代码如下:

                $validator = Validator::make(
            array('email' => 'required|min:7'),
            array('password' => 'required|min:7'),
            array('firstName' => 'required'),
            array('lastName' => 'required'));

        if ($validator->fails())
        {
            // The given data did not pass validation
            /*Get error msgs from validator*/

           return Redirect::to('members.registration')->withErrors($validator);

        }

传递给Redirect :: to的参数是驻留在其中的文件夹成员和注册视图。问题是这条线特别引起的:

   return Redirect::to('members.registration')->withErrors($validator);

当它被注释掉时,表单提交会返回一个空白页面。否则,图片中的以下错误将显示为enter image description here

路线文件包含以下内容:

Route::get('/', 'MainController@index');
Route::get('membersaccess', array('as' => 'membersaccess', 'uses' => 'MainController@loadMembersAccess'));
Route::get('signin', array('as' => 'signin', 'uses' => 'MembersController@loadlogin'));
Route::get('signup', array('as' => 'signup', 'uses' => 'MembersController@loadRegistration'));
Route::post('postLogin', array('as' => 'postLogin', 'uses' => 'MembersController@login'));
Route::post('postRegistration', array('as' => 'postRegistration', 'uses' => 'MembersController@registration'));
包含验证部分的

函数是:

public function registration()
    {
        $email = Input::get('email');
        $password = md5(Input::get('password'));
        $firstName = Input::get('firstName');
        $lastName = Input::get('lastName');
        $country = Input::get('country');

        //echo $email;

        $validator = Validator::make(
            array('email' => 'required|min:7'),
            array('password' => 'required|min:7'),
            array('firstName' => 'required'),
            array('lastName' => 'required'));

        if ($validator->fails())
        {
            // The given data did not pass validation
            /*Get error msgs from validator*/

           return Redirect::to('members.registration')->withErrors($validator);

        }
    }

以及参考表格:

@if(Session::has('errors'))
    <? $errors = Session::get('errors'); ?>
        <h3> {{ $errors->first('email') }}</h3>
@endif

{{ Form::open(array('route' => 'postRegistration')) }}

{{ Form::text('email', null, array('placeholder'=>'Email', 'class' => 'randomfieldsize' ) ) }}
{{ Form::password('password', array('placeholder'=>'Password', 'class'=>'randomfieldsize' ) ) }}
{{ Form::text('firstname', null, array('placeholder'=>'First Name', 'class' => 'randomfieldsize' ) ) }}
{{ Form::text('lastName', null, array('placeholder'=>'Last Name', 'class' => 'randomfieldsize' ) ) }}   
{{ Form::select('country', array('' => '', 'saudi' => 'Saudi Arabia', 'uae' => 'UAE')) }} <br><br>


{{Form::submit('Proceed', ['class' => 'button [radius round]'])}}


{{ Form::close() }}

1 个答案:

答案 0 :(得分:1)

试试这个:

return Redirect::route('signup')->withErrors($validator);

您没有将路线定义为 members.registration ,因此可能会出现问题。

为了显示错误,我通常使用它(使用引导程序设置样式):

@if( $errors->has() )
<div class="alert alert-danger alert-dismissable">
    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
    <ul>
    @foreach ( $errors->all('<li>:message</li>') as $error )
        {{ $error }}
    @endforeach
    </ul>
</div>
@endif

编辑:呃,之前没有注意到,但你的验证码是错误的。请参考http://laravel.com/docs/validation它应该是这样的:

    $email = Input::get('email');
    $password = Input::get('password'); // Better to hash the password in another place, since md5 can create a hash even of an empty string. Also, please use laravel hash utility instead of md5: http://laravel.com/docs/security#storing-passwords
    $firstName = Input::get('firstName');
    $lastName = Input::get('lastName');
    $country = Input::get('country');

    $validator = Validator::make(
        compact('email', 'password', 'firstName', 'lastName', 'country'),
        array(
            'email' => 'required|min:7',
            'password' => 'required|min:7'
            'firstName' => 'required'
            'lastName' => 'required'
        ));