【问题标题】:CodeIgniter Form Validation IssueCodeIgniter 表单验证问题
【发布时间】:2017-01-24 22:45:46
【问题描述】:

所以我正在使用代码点火器验证表单,这大大简化了我的代码,但是我面临一个新问题。我的表单首先被加载为视图内的弹出窗口。但是当控制器在验证后返回响应时,表单会作为单独的网页打开,而不是仅仅加载到前一个视图中。

main_view.php

<script>
 function div_show(type, classID) {    
   if(type=='adduser')
   {
     document.getElementById('AddUser_popup').style.display = "block";
     $("#AddUser_popup").load("add_user");
   }
 }
</script>
<body>
  <button id="popupNewTerm" onClick="div_show('adduser', null)">Add user</button>
  <div class="AddUser_popup" id="AddUser_popup">  </div>
</body>

控制器:

public function add_user()
{
    $data = array();
    $this->load->helper('form');
    $this->load->helper('url');
    $this->load->library('form_validation');
    $this->load->model('user_m');
    $this->form_validation->set_rules('username','Username', 'required|trim');
    $this->form_validation->set_rules('emp_email','E-mail', 'required|trim|valid-email|xss_clean');

    if($this->form_validation->run()==FALSE)
    {
        $this->load->view('includes/forms/add_user', $data);
    }
    else {
        $data['username']=$this->input->post('username');
        $data['emp_email']=$this->input->post('emp_email');


        $user=array(
            'user_id'=> NULL,
            'username'=> $data['username'],
            'emp_email'=>$data['emp_email']
        );
        $this->user_m->insert_user($user);
        $this->load->view('includes/forms/add_user', $data);
    }
}

表单-> (add_user.php)

<div id="popupContact">
 <?php

 if(isset($username) && isset($emp_email))
 {
     echo validation_errors();
     echo 'User added successfully!';
 }
 else {
    echo validation_errors();
    echo form_open('', 'id="form" name="form"');
    echo '<p id="close" onclick ="div_hide()">X</p>';
    echo '<h2>Add User</h2>';
    echo '<hr>';
    echo '<label for="username">Username: </label>'.form_input('username', set_value('username')) .'&nbsp;&nbsp;';
    echo '<label for="emp_email">Email: </label>'.form_input('emp_email', set_value('emp_email')) . '<br><br>';


    echo form_submit('submit', 'Submit', 'id="submit"');

    echo form_close();
 }
 ?>
</div>

如何在控制器验证失败后在 main_view.php 中加载表单,并且当验证成功时,我希望表单在主页内关闭。我可以使用普通的 javascript 和 php 进行表单验证,但想学习代码点火器的验证方法。谢谢。

【问题讨论】:

    标签: php jquery validation codeigniter-3


    【解决方案1】:

    如果您在验证中遇到有关 Codeigniter 的问题,请阅读CodeIgniter Official Form Validation Guide

    我建议将验证规则集保存到配置文件

    如果您仍然对阅读/查看以下代码感到困惑,它将对您有所帮助。此代码用于配置文件。

    <?php
    
    $config = array(
        'master/user' => array(
            array(
                'field' => 'empname',
                'label' => 'Employee Name',
                'rules' => 'required|trim|min_length[6]|xss_clean'
            ),
            array(
                'field' => 'cnt',
                'label' => 'Contact Number',
                'rules' => 'required'
            ),
            array(
                'field'=>'dob',
                'label'=>'Date Of Birth',
                'rules'=>'required'
            ),
            array(
                'field' => 'design',
                'label' => 'Designation',
                'rules' => 'required'
            ),
            array(
                'field'=>'pass',
                'label'=>'Password',
                'rules'=>'required'
            ),
            array(
                'field' => 'cpass',
                'label' => 'Password Confirmation',
                'rules' => 'required|trim|min_length[6]|matches[pass]|xss_clean'
            ),
            array(
                'field' => 'email',
                'label' => 'Email',
                'rules' => 'required'
            ),
    
    //        array(
    //            'field' => 'prof',
    //            'label' => 'Profile Image',
    //            'rules' => 'required|callback_upload_image'
    //        )
        ),
        'master/SAdmin' => array(
            array(
                'field' => 'empname',
                'label' => 'Employee Name',
                'rules' => 'required|trim|min_length[6]|xss_clean'
            ),
            array(
                'field' => 'cnt',
                'label' => 'Contact Number',
                'rules' => 'required'
            ),
            array(
                'field'=>'dob',
                'label'=>'Date Of Birth',
                'rules'=>'required'
            ),
            array(
                'field' => 'design',
                'label' => 'Designation',
                'rules' => 'required'
            ),
            array(
                'field'=>'pass',
                'label'=>'Password',
                'rules'=>'required'
            ),
            array(
                'field' => 'cpass',
                'label' => 'Password Confirmation',
                'rules' => 'required|trim|min_length[6]|matches[pass]|xss_clean'
            ),
            array(
                'field' => 'email',
                'label' => 'Email',
                'rules' => 'required'
            )
    
        ),
        'master/task' => array(
            array(
                'field' => 'jtitle',
                'label' => 'Job Title',
                'rules' => 'required|trim|xss_clean'
            ),
            array(
                'field' => 'jnature',
                'label' => 'Job Type',
                'rules' => 'required'
            ),
            array(
                'field'=>'assigne',
                'label'=>'Assigned From',
                'rules'=>'required'
            ),
            array(
                'field' => 'assignto',
                'label' => 'Assign To',
                'rules' => 'required'
            ),
            array(
                'field'=>'ddate',
                'label'=>'Due Date',
                'rules'=>'required'
            ),
            array(
                'field' => 'reminder',
                'label' => 'Reminder',
                'rules' => 'required|trim|max_length[1]|xss_clean'
            ),
    //        array(
    //            'field'=>'image',
    //            'label' => 'Image',
    //            'rules' => 'required'
    //        )
    //       
    
        )
    );
    
    ?>
    

    【讨论】:

    • 感谢您的建议
    猜你喜欢
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多