Syed_18 0 Newbie Poster

Hi

I have problem that cannot show form validation errors in view . I am using ajax to post data to controller.

Here is my Controller's Validation part

private function _validate()
    {

        $this->form_validation->set_rules('Name', 'Name', 'required|alpha');
        $this->form_validation->set_rules('CellNo', 'CellNo', 'required|numeric');
        $this->form_validation->set_rules('Address', 'Address', 'required');
        $this->form_validation->set_rules('Rate', 'Rate', 'required|numeric');
        $this->form_validation->set_rules('Advance', 'Advance', 'required|numeric');
        $this->form_validation->set_rules('BottlesQty', 'BottlesQty', 'required|numeric');
        $this->form_validation->set_rules('DeliveryDay', 'DeliveryDay', 'required');
        $this->form_validation->set_rules('DeliveryPerson', 'DeliveryPerson', 'required');
        $this->form_validation->set_rules('referenceName', 'referenceName', 'required');

        if ($this->form_validation->run() == FALSE) {
            $errors = validation_errors();

            echo json_encode(['error'=>$errors]);}
}

Here is view with ajax

<div id="print-error-msg" class="alert alert-danger " style="display:none"></div>

//part of ajax posting to controller
var formData = new FormData($('#form')[0]);
    $.ajax({
        url : url,
        type: "POST",
        data: formData,
        contentType: false,
        processData: false,
        dataType: "JSON",
        success: function(data)
        {
          swal({ // SHOW SWEET ALERT IF CUSTOMER ADDED
                        title: "Customer Added/Updated!",
                        text: "Customer Added/Updated",
                        type: "success",
                        timer: 2000,
                        showConfirmButton: true,
                        allowOutsideClick: false,
                    });

                    if($.isEmptyObject(data.error)){
                $("#print-error-msg").css('display','none');
                alert(data.success);
                }else{
                    alert(data.error);

$(".print-error-msg").css('display','block');
                $("#print-error-msg").html(data.error);
                }
},

I need to show form validation error messages under the input fields.

Please help me to resolve this. Thanks in advance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.