Validation errors in AJAX mode

前端 未结 8 985
盖世英雄少女心
盖世英雄少女心 2021-01-30 00:14

Currently I use this to display validation errors via ajax:

            if (data.validation_failed == 1)
            {
                var arr = data.errors;
            


        
8条回答
  •  自闭症患者
    2021-01-30 00:55

    I made it, try this hope it will help you with rendering errors after the relevant fields.

    $("#booking_form").submit(function(e){
                    e.preventDefault();
                    let form_data = $("#booking_form").serialize()
                    $(document).find("span.text-danger").remove();
                    $.ajax({
                        url : "your-url",
                        type : "POST",
                        data : form_data,
                        success : function(response){
    
                        },
                        error:function (response){
                            $.each(response.responseJSON.errors,function(field_name,error){
                                $(document).find('[name='+field_name+']').after('' +error+ '')
                            })
                        }
                    })
                })
    

提交回复
热议问题