【问题标题】:Code igniter form validation before opening modal window打开模式窗口之前的 Codeigniter 表单验证
【发布时间】:2016-06-15 09:33:08
【问题描述】:

我目前有一个表单,我希望为其实现表单验证。我正在使用代码点火器框架。

当我点击提交表单按钮时,表单并没有提交,而是弹出一个模式窗口,其中包含用户刚刚输入的信息(有点像确认他们输入了正确的详细信息。) 当用户在模态窗口中单击“确定”时,表单然后通过 ajax 提交,并弹出另一个模态窗口以确认表单已提交。

我想做的是在第一个模式窗口弹出之前验证表单,但我似乎找不到这样做的方法。有谁知道我该怎么做?

这是我的表格:

<?php if(isset($error)){
echo "<i>" . $error . "</i>";
}?>
<form id ="form1" name ="form1">
    <div class="hospital_container">
        <h3 class = "hospital_header"><b>Enter Hospital Information</b></h3>

    <label for="hospitalName"  id ="hospLabel" class = "labelForm">Name:</label>
    <input type="text" class = "input2" id="hospitalName" name="hospitalName" class = "newUserForm">

        <button type="button"  id = "hospital_submit_button" class = "hospital_submit_button" data-toggle="modal" data-target="#userModal">Add New Hospital</button> // triggers the modal window but I want to run form validation before this.

    </div>
</form>

Ajax 提交:

     $(function() {
//#overlaysubclass2 is the name of the submit button in the modal window
            $("#overlaysubclass2").on("click", function(event) {
                event.preventDefault();
                var DataString=$("#form1").serialize()
                $.ajax({
                    url: "<?php echo base_url(); ?>index.php/Admin/createHospital",
                    type: "post",
                    data:DataString ,
                    success: function(data) {

                        $("#userModal").modal('hide'); // hide the original modal and show the confirmation modal
                        $("#confirmModal").modal('show');
                    }


                });
            });
        });

我的控制器:

 function createHospital(){
    $this->load->library('form_validation');

    $this->form_validation->set_rules('hospitalName', 'hospitalName', 'required');
  //  $this->form_validation->set_rules('hospAddress', 'hospAddress', 'required');


    if ($this->form_validation->run() === FALSE) {
        $data['error'] = 'Please fill in the form';
        $this->load->view('includes/admin/header');
       $this->load->view('newHospital');

    }else {
        $data = array(

            'hospitalName' => $this->input->post('hospitalName'),
        );

        $this->user_model->create_hospital($data);
      //$data['message'] = 'Data Inserted Successfully';
        $this->load->view('newHospital');
    }
}

表单验证实际上并没有运行,尽管它允许我提交一个空白表单

非常感谢任何帮助,因为我真的被困住了!

【问题讨论】:

  • 在我看来,在您准备好将数据保存到数据库之前,验证是毫无意义的。在您的工作流程中,必须将字段数据两次传递到服务器 - 一次用于验证它,第二次用于保存它。换句话说,您将需要两个 ajax 调用;一个在单击#hospital_submit_button 时处理表单提交,另一个在单击#overlaysubclass2 时保存数据。请记住,每个 ajax 调用都会创建 CodeIgniter 框架的全新实例。你真的想对你的服务器造成那么大的打击吗?
  • 是的,我想你是对的@DFriend .. 我决定使用 JQuery 和 Javascript 进行一些手动表单验证!不过感谢您的建议!

标签: ajax forms codeigniter validation modal-dialog


【解决方案1】:

“虽然它允许我提交一个空白表单,但表单验证实际上并没有运行”实际上你不知道你的验证是否正确运行。

首先你需要把你的响应(从ajax调用到:createHospital方法):

$("#confirmModal").html(data);  // this line put your response html to '#confirmModal' dom elem.
$("#confirmModal").modal('show');

第二个更好的选择:

$("#overlaysubclass2").on("click", function(event) {

是:

$("#form1").on("submit", function(event) { 

或:

$("#hospital_submit_button").on("click", function(event) {

【讨论】:

  • 嘿@paweIn!感谢您的回复。我需要在第一个模式窗口之前进行表单验证,而不是确认模式。你知道我会怎么做吗?再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 2014-09-11
  • 1970-01-01
  • 2016-03-20
  • 2018-11-25
  • 1970-01-01
相关资源
最近更新 更多