简体   繁体   English

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

[英]How to add form validation error in Bootstrap modal

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. 我想在Bootstrap模式中显示表单验证错误(在输入字段下面),我之前已经完成了,但是现在因为我使用ajax发送数据我在显示验证错误时遇到问题。

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");
        }
}

If your validation is false you want to return json object with the validation errors: 如果验证为false,则需要返回带有验证错误的json对象:

$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. 现在,您将在ajax成功中使用验证错误数组接收此响应对象,然后您可以遍历此数组并显示您想要的错误。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Bootstrap 3表单模式,使用PHP进行验证 - Bootstrap 3 form modal, using PHP, validation Bootstrap模式表单提交错误 - Bootstrap Modal Form submit error 如何通过使用jQuery将表单数据添加到同一页面的引导程序模式中? - How to add form data to a bootstrap modal in the same page by using jQuery? DOM加载后验证引导程序模态形式和模态载荷 - validation of bootstrap modal form and modal loads after DOM loaded 如何在带有 Bootstrap 表单的模式中给出友好的错误消息? - How to give a friendly error message inside a modal with Bootstrap form? AuthController发现验证错误时,如何重定向到“注册模式”(bootstrap modal)窗口? - How to redirect to Registration modal(bootstrap modal) window when AuthController found validation error/'s? 使用 Bootstrap Modal 表单添加不起作用 - ADD using Bootstrap Modal form is not working Bootstrap Modal Popup Jquery Post验证如何? - Bootstrap Modal Popup Jquery Post validation how? Bootstrap 模式中的 WordPress 自定义联系表单未显示验证和提交响应 - WordPress custom Contact Form in Bootstrap modal is not showing validation & submission response 结合验证(js),提交(php)和模式(bootstrap)的形式 - Combine Validation (js), submit (php) and modal (bootstrap) in form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM