输入验证不会提示错误

时间:2016-06-28 15:58:24

标签: javascript php jquery validation laravel

如果我单击提交按钮它只是刷新浏览器并且不会验证我的输入或者我不确定Javascript是否导致验证将无效,似乎我的验证将无法正常工作。首先,我将从使用基于用户选项的empty()append()函数的其他文件中添加我的注册表开始。

@section ('page-script')

<script>

    $(document).on("click", "#register", function ()
    {
        $.get("accountRegister", function (data)
        {
            $("#accountBottomContainer").empty();
            $("#accountBottomContainer").append(data);
        });
    });
 </script>

 @endsection

正如您在此处所见,我在名为panel-body <{p>的#accountBottomContainer中追加 register.blade.php

Screenshot

如果用户点击register按钮后,我会附加我的 register.blade.php panel-body按下id = accountBottomContainer < / p>

@section ('content')

<div class = "row">

    <div class = "col-xs-12 col-md-8">

        <div class = "panel panel-default">

            <div class = "panel-heading">

                <h2>Account Dashboard</h2>
                <hr>

            </div>

            <div class = "panel-body">

                <button type = "button" class = "btn btn-info" id = "register">Register Employee</button>
                <button type = "button" class = "btn btn-info" id = "searchEmployee">Search Employee</button>

                <div class = "row">
                <br>
                    <div class="col-md-10">

                        <div class = "panel panel-default">

                            <div class = "panel-body" style = "height: 500px" id = "accountBottomContainer">


                            </div>

                        </div>

                    </div>
                </div> 

            </div>

        </div>

    </div>

</div>

@endsection

表单验证

这里我遍历每个错误以检查用户错过填写其中一个文本字段的位置。

@if (count($errors) > 0)


<div class = "row">
    <div class = "col-md-6">
        <ul>
            @foreach($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
</div>

@endif
<form class = "form-vertical" role = "form" method = "post" action = "{{ route ('account.accountRegister') }}">


    <div class = "form-group">

        <label for = "email" class = "control-label">Email Address</label>
        <input type = "text" name = "email" class = "form-control" id = "email">

    </div>

    <div class = "form-group">

        <label for = "username" class = "control-label">Username</label>
        <input type = "text" name = "username" class = "form-control" id = "username">

    </div>



    <div class = "form-group">

        <label for = "password" class = "control-label">Password</label>
        <input type = "password" name = "password" class = "form-control" id = "password">

    </div>

    <div class = "form-group">

        <button type = "submit" class = "btn btn-default">Sign Up</button>

    </div>

    <input type = "hidden" name = "_token" value = "{{ Session::token() }}">

</form>

控制器

public function postRegister(Request $request)
{
    $this->validate($request, [
                            //This will be unique in users table
        'email' => 'required|unique:users|email|max:255',
        'username' => 'required|unique:users|alpha_dash|max:20',
        'password' => 'required|min:6',
    ]);
 }

路由

Route::get('/accountRegister',
[
    'uses' => '\App\Http\Controllers\AccountController@getRegister',
    'as' => 'account.accountRegister',
]);

Route::post('/accountRegister', 
[
    'uses' => '\App\Http\Controllers\AccountController@postRegister',
]);

1 个答案:

答案 0 :(得分:0)

在您的javascript上,您使用#register ID,但我无法在html端看到它。这是一个错误吗? 你应该使用:

$(document).on("submit", "#theIdOfYourFormTag", function () {