doufei7516 2019-06-10 19:38
浏览 64

如何在Bootstrap模式中添加表单验证错误

I want to display form validation error in the Bootstrap modal (below the input fields), I have done it previously, but now since I am using ajax to send the data I am having trouble displaying the validation error.

Please Help. Thanks in advance

**View**

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <form id="myform">
            <div class="modal-body">
            <?php echo validation_errors(); ?>
               <input class="form-control" type="text" name="user_name" id="user_name">
                <input class="form-control" type="text" name="name" id="name">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-primary" id="add">Save changes</button>
            </div>
            </form>
        </div>
    </div>
</div>

<script>
    $(document).ready(function () {
        $(document).on('submit','#myform',function(event){
            event.preventDefault();
            let name=$("#name").val();
            let user_name=$("#user_name").val();
             $.ajax({
                    url:"<?php echo base_url(). 'welcome/add_data';?>",
                    method: 'POST',
                    data:new FormData(this),
                    contentType:false,
                    processData:false,
                    success:function (data) {
                        alert(data);
                        $('#myform')[0].reset();
                        $('#myModal').modal('hide');
                       location.reload();
                    }
                });
        })
    })
</script>

**Controller**

public function add_data(){
        $this->form_validation->set_rules('name', 'Name', 'required');
        $this->form_validation->set_rules('user_name', 'User Name', 'required');
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('main_view');
        }
        else {
            $data=array(
                'name'=>$this->input->post('name'),
                'user_name'=>$this->input->post('user_name'),
            );
            $this->Test_model->add_data($data);
            redirect('welcome',"refresh");
        }
}
  • 写回答

1条回答 默认 最新

  • douguiyan9164 2019-06-10 22:14
    关注

    If your validation is false you want to return json object with the validation errors:

    $response['errors'] = $this->form_validation->error_array();
    header('Content-Type: application/json');
    echo json_encode($response);
    

    Now you will receive this response object in your ajax success with a validation errors array, you can then loop through this array and display the error however you want.

    $.each(data.errors, function(index, error) {
        alert(error);    // or maybe display it in a bootstrap alert
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试