简体   繁体   中英

Validation won't work using blade template

I'm trying to validate my form but when I hit the submit button it always refresh the page and didn't validate anything. So the first process start at the

accountDashboard.blade.php

@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>

accountDashboard.blade.php (routes)

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

As you can see above I have two button one for the register and searchRegister . For instance I hit #register button the blade template will append it on my second panel-body with the id of #accountBottomContainer . Script below of my javascript.

<script>

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

</script>

accountRegister.blade.php

This is my blade template that I will append from panel-body which works fine I'm just having problem with the validation.

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


<div class = "form-group {{ $errors->has('email') ? ' has-error' : '' }}">

    <label for = "email" class = "control-label">Email Address</label>
    <input type = "text" name = "email" class = "form-control" value = "{{ Request::old('email') ?: '' }}">

    @if ($errors->has('email'))
        <span class = "help-block">{{ $errors->first('email') }}</span>
    @endif

</div>

<div class = "form-group {{ $errors->has('username') ? ' has-error' : '' }}">

    <label for = "username" class = "control-label">Username</label>
    <input type = "text" name = "username" class = "form-control" value = "{{ Request::old('username') ?: '' }}">

    @if ($errors->has('username'))
        <span class = "help-block">{{ $errors->first('username') }}</span>
    @endif

</div>



<div class = "form-group {{ $errors->has('password') ? ' has-error' : '' }}">

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

    @if ($errors->has('password'))
        <span class = "help-block">{{ $errors->first('password') }}</span>
    @endif


</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>

Controller

public function postRegister(Request $request)
{
    //VALIDATION
    $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',
    ]);

    User::create
    ([

        'username' => $request->input('username'),
        'email' => $request->input('email'),
        'password' => bcrypt($request->input('password')),
    ]);

    return redirect()->route('account.accountRegister');
}

routes for accountRegister.blade.php

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

Route::post('/accountRegister', 
[
   'uses' => '\App\Http\Controllers\AccountController@postRegister',
]);
<div class="form-group @if($errors->has('username')) has-error @endif">
     <label for="username" class="col-sm-2 control-label"></label>
          <div class="col-sm-10">
              <input type="text" class="form-control" name="username" value="{{ Request::old('username') ?: '' }}" readonly="readonly" />
                  @if($errors->has('username'))
                     <span class="help-block">{{ $errors->first('username') }}</span>
                  @endif
       </div>
 </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM