验证无法正常工作 - 即使向字段插入值,也会显示该字段

时间:2018-04-21 17:50:08

标签: php laravel

当用户点击"转到步骤3"它出现在" console.log(errorsHtml); "这个:

<ul class="alert alert-danger mt-3"><li class="text-danger">The payment method field is required.</li></ul>

即使用户选择付款方式,也会显示验证错误。你知道这可能是什么问题吗?

HTML:

 <form method="post" id="step2form" action="">
    <h6>Payment method</h6>
    <div class="form-group">
        <div class="form-check">
            <input class="form-check-input" type="radio" name="payment_method" value="option1">
            <label class="form-check-label d-flex align-items-center" for="exampleRadios1">
                <span class="mr-auto">Payment method 1</span>
            </label>
        </div>
        <br>
        <div class="form-check">
            <input class="form-check-input" type="radio" name="payment_method" value="option1">
            <label class="form-check-label d-flex align-items-center" for="exampleRadios1">
                <span class="mr-auto">Payment method 2</span>
            </label>
        </div>
    </div>
    <br>
    <div class="text-right">
    <button type="button" href="#step3" data-toggle="tab" role="tab"
            class="btn btn-outline-primary prev-step">
        Go back to step 2
    </button>
        <input type="submit" href="#step2" id="goToStep3"
         class="btn btn-primary btn float-right next-step"
               value="Go to step 3"/>
    </div>
</form>

的Ajax:

$('#goToStep3').on('click', function (event) {
                event.preventDefault();

                var custom_form = $("#" + page_form_id);

                $.ajax({
                    method: "POST",
                    url: '{{ route('products.storePaymentMethods', compact('id','slug') ) }}',
                    data: custom_form.serialize(),
                    datatype: 'json',
                    success: function (data, textStatus, jqXHR) {
                    },
                    error: function (data) {

                        var errors = data.responseJSON;
                        var errorsHtml = '';
                        $.each(errors['errors'], function (index, value) {
                            errorsHtml += '<ul class="alert alert-danger mt-3"><li class="text-danger">' + value + '</li></ul>';
                            console.log(errorsHtml);
                        });

                        $('#response').show().html(errorsHtml);
                    }
                });

PaymentController方法:

public function storePaymentMethods(Request $request, $id, $slug = null, Validator $validator){

        $validator = Validator::make($request->all(),[
            'payment_method' => 'required',
        ]);

        if($validator->passes())

        {
            return response()->json([
                'success' => true,
                'message' => 'success'
            ], 200);
        }
        $errors = $validator->errors();
        $errors =  json_decode($errors);

        return response()->json([
            'success' => false,
            'errors' => $errors
        ], 422);
    }

1 个答案:

答案 0 :(得分:0)

更改为此并尝试

public function storePaymentMethods(Request $request, $id, $slug = null){

    $validatedData = $request->validate([
        'payment_method' => 'required',
    ]);
    return response()->json([
       'success' => true,
       'message' => 'success'
      ], 200);

    }
相关问题