确认密码验证在 Laravel 中不起作用

时间:2021-03-24 11:08:03

标签: php laravel

我的项目基于 laravel 8 和 php 7.4。

我有一个基于 Ajax 的注册表单。我要求用户输入密码,然后确认密码。我正在处理 json 响应中的验证。我的所有字段都经过正确验证并且可以正常工作,但我不知道我应该如何验证确认密码字段以匹配密码。

刀片

<form id="register_forms">
       <div>
       <label for="register_password"></label>
       <input placeholder="password" id="register_password" type="password" class="input form-control @error('register_password') is-invalid @enderror" name="register_password" required autocomplete="register_password">
    
        <span class="message" id="register_password" style="color: #ff0000; display:none;" role="alert"><strong>{{ __('The password field is required.')}}</strong></span>
       </div>
    
       <div>
        <label for="register_password_confirmation"></label>
         <input placeholder="confirm Password" type="password" class="input form-control" name="register_password_confirmation" required autocomplete="new-password">
       </div>
           
   <input id="my-register" type="submit" value="Continue">

</form>

JavaScript

<script>
   $("#my-register").click(function(e){
    e.preventDefault();
      var _token = $('input[name="_token"]').val();
      var data = $('#register_forms').serialize()
      $.ajaxSetup({
               headers: {
                   'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
               }
           });
            $.ajax({
        type: "post",
        beforeSend: function(){
                    $('.ajax-loader').css("visibility", "visible");
                },
        url: "{{ route('register') }}",
         data: $('#register_forms').serialize(),
       //data : $('#register_forms').serialize() + "&_token=" + _token,

                dataType: 'json',
        success: function(data) {
            window.location = "https://carchainclassics.com/login";
       },
         complete: function(){
                    $('.ajax-loader').css("visibility", "hidden");
         },

       error: function (err) {
            $('.message').hide();
            if (err.status == 422) {
                $('#success_message').fadeIn().html(err.responseJSON.message);
                // display errors on each form field
                $.each(err.responseJSON.errors, function (i, error) {
                    grecaptcha.reset();
                    var el = $(document).find('span[id="'+i+'"]');
                    el.show();
                    
                });
            }
        }
    });
    /**Ajax code ends**/
        });


</script>

控制器

protected function validator(array $data)
{
        'register_password' => ['required','string', 'min:8','confirmed'],
    ]);
    $validator->setAttributeNames([
        'register_password' => 'password',
    ]);
    return $validator;
}

1 个答案:

答案 0 :(得分:0)

@shaan 你可以试试这个

$this->validate($request, [
'name' => 'required|min:3|max:50',
'email' => 'email',
'vat_number' => 'max:13',
'password' => 'min:6|required_with:password_confirmation|same:password_confirmation',
'password_confirmation' => 'min:6'
]);