laravel5.4验证无法显示$错误

时间:2017-06-05 08:51:51

标签: forms laravel validation

我使用验证来确认注册表格。 `$ message = array('required'=>'你不能');

    $datavalidate = array(
        'phone' =>$request->input('phone')
    );
    $rules = array(
        'phone' =>'required'
    );

    $validator = Validator::make($datavalidate,$rules,$message);
    if($validator->fails())
    {
        return Redirect::back()->withErrors($validator);

    }

    $this->validate($request,[
        'phone' => 'required',
        'confirmCode' => 'required'
    ]);`

两种方式都无法获得$ error。  我可以肯定这个验证工作。抛出异常,因为在验证方法之后,所有代码都不能成为借口。  但我可以得到$错误,也许是直接错误的网址? 这包括在注册网站。 `

    <ul>
        <li>hello</li>
        @foreach($errors->all() as $error)
            <li>{{$error}}</li>
            @endforeach
    </ul>
{{var_dump($errors)}}

此列表无法显示任何$ error.Is没有直接的最后一个网址。 更新:这是形式。

            <form class="form-horizontal" action="{{route('confirmRegister')}}"method="post" onsubmit="return postForm()">

            {{csrf_field()}}
            <div class="form-group">
                <label for="mobileNum" class="col-xs-3 control-label text-center">手机号</label>
                <div class="col-xs-8">
                    <input type="text" name="phone" class="form-control" id="mobileNum" placeholder="手机号码">
                </div>
            </div>

            <div class="form-group">

                <label for="passWord" class="col-xs-3 text-center">密码</label>
                <div class="col-xs-8">
                    <input type="password" class="form-control" id="passWord" placeholder="密码">
                    <input type="hidden" id="sendPassWord" name="passWord">
                    <input type="hidden" id="openId" name="openId">
                </div>
            </div>

            <div class="form-group">

                <label for="confirmPassWord" class="col-xs-3 text-center">确认密码</label>
                <div class="col-xs-8">
                    <input type="password" name="confirmPassWord" class="form-control" id="confirmPassWord" placeholder="确认密码">
                </div>
            </div>

            <div class="form-group">
                <label for="confirm" class="col-xs-3 text-center">验证码</label>
                <div class="col-xs-5">
                    <input type="text" name="confirmCode" class="form-control" id="confirm" placeholder="短信验证码">
                </div>
                <div class="col-xs-4">
                    <button type="button" class="btn btn-info btn-block" id="getConfirm">获取验证码</button>
                </div>
            </div>
            <div class="alert alert-danger alert-dismissible col-xs-offset-3 col-xs-8" role="alert" style="display: none" id="codeError">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <strong>错误!</strong> 获取验证码失败!
            </div>


            <div class="form-group">
                <div class="col-xs-offset-3 col-xs-8">
                    <button type="submit" class="btn btn-primary btn-block" id="register">注册</button>
                </div>
            </div>





        </form> 

更新:

        function postForm() {

        var passWord = md5( $('#passWord').val() );
        $('#sendPassWord').val(passWord);

        $('#openId').val("{{$openId}}");


        return true;


    } 

1 个答案:

答案 0 :(得分:0)

    phone<input type="text" name="phone" >
    <span class="error">{{$errors->first('phone')}}</span>

    confirmCode<input type="text" name="confirmCode">
    <span class="error">{{$errors->first('confirmCode')}}</span>

控制器代码

     $rules = ['phone' => 'required',
            'confirmCode' => 'required',

        ];
        $message = [
            'phone.required' => 'enter your phone number',
            'confirmCode.required' => 'confirm your code

        ];
        $validator = validator::make($request->input(), $rules, $message);
        if ($validator->fails()) {
            return back()
                ->WithErrors($validator)
                ->WithInput();
}else{    write your codes here

试试这个。以这种方式设置规则和消息