验证不适用于 HMVC Codeigniter

Validation does not work on HMVC Codeigniter

我是 HMVC 的新手 Codeigniter.I 会使用 codeigniter 对 codeigniter 的 HMVC 格式进行表单验证,但它没有显示任何效果,这意味着 formvalidation 不适用于我的项目。但是这段代码应该适用于 MVC codeigniter。 所以请帮我在我的项目中解决这个问题 project.I 非常感谢在我的项目中帮助解决这个问题的人。

** 我有一个关联控制器文件 feedback.php 如下**

 function index($offset=0){
         $this->load->helper('url');
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('name','Name','trim|required');

        $this->form_validation->set_rules('email','Email Address','trim|valid_email|required');
        $this->form_validation->set_rules('message','Message','trim|required');

            if($this->form_validation->run()){

                $data1=array(
                    'name' => $this->input->post("name"),

                    'email' => $this->input->post("email"), 

                    'message' => $this->input->post("message"),

                );


                }

            }
            $data=array('body1'=>'feedback');
             $this->load->view('temp',$data);

        }

我有一个关联视图文件feedback.php如下

<form action="<?php echo site_url()?>"  name="FeedbackForm" method="post">

   <span style="color:#F00">

    <?php echo validation_errors(); ?>

    </span>
                    <table>

                        <tr>
                        <td><label>Name</label></td>
                        <td><input  id="name" name="name" type="text" /></td>
                        </tr>

                        <tr>
                        <td><label>Email</label></td>
                        <td><input type="email" name="email" id="email" /></td>
                        </tr>

                        <tr>
                        <td><label>Message</label></td>
                        <td><textarea name="message" rows="2" cols="16" ></textarea></td>
                        </tr>

                        <tr>
                        <td></td>
                        <td><input type="submit" id="submit" value="Send" /> </td>
                        </tr>

                    </table>

                    </form>

在某些使用 HMVC 的情况下,您可能需要使用 MY_Form_Validation 库。如果在 HMVC 的表单验证中也使用回调将不起作用,除非有下面的代码。

如果学习最好使用 codeigniter 2.2.1 版本,codeigniter 3 出来了,但仍然很少有错误。

另一件需要注意的事情是您可能需要在 config/routes.php

中配置您的路由
$route['feedback'] =  "module-folder-name/feedback/index";
$route['feedback/updates/(:any)'] =  "module-folder-name/feedback/updates/";
$route['feedback/add'] =  "module-folder-name/feedback/add";
$route['feedback/delete'] =  "module-folder-name/feedback/delete";

在表单更改站点 url 到 base_url() base_url 以及在 routes.php[= 中设置的控制器名称20=]

在你的表格上

<?php echo base_url('feedback')?>

此外,如果需要从 url.

获取 ID,为什么需要 $offset=0 查看 uri 段
<?php

class MY_Form_validation extends CI_Form_validation {

    function run($module = '', $group = '') {
        (is_object($module)) AND $this->CI = &$module;
        return parent::run($group);
    }

} 

然后在控制器中将是 run($this)

class Feedback extends MX_Controller {

public function index() { 

$this->load->helper('url'); // Try autoloading it
$this->load->helper('form'); // Try autoloading it
$this->load->library('form_validation');

$this->form_validation->set_rules('name','Name','trim|required');

$this->form_validation->set_rules('email','Email Address','trim|valid_email|required');
$this->form_validation->set_rules('message','Message','trim|required');

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

// Load Main View & Data.

$this->load->view('folder/feedback');    

} else {

// Load Success View Or Redirect

$this->load->model('module-name/model_feedback');

$this->model_feedback->update();

// Or

$this->model_feedback->insert();

redirect('controller-name');    

}

}

}

型号

public function update() {
$data = array(
'username' => $this->input->post('email'),
'email' => $this->input->post('email'),
'message' => $this->input->post('message')
);

$this->db->where('your_id', $your_id); // May be uri segment() etc read userguide
$this->db->update('tablename', $data);
}

public function insert() {
$data = array(
'username' => $this->input->post('email'),
'email' => $this->input->post('email'),
'message' => $this->input->post('message')
);

$this->db->insert('tablename', $data);
}

查看

<form action="<?php echo base_url('feedback')?>"  name="FeedbackForm" method="post">

<?php echo validation_errors(); ?>

<table>
<tr>
<td><label>Name</label></td>
<td><input  id="name" name="name" type="text" /></td>
</tr>

<tr>
<td><label>Email</label></td>
<td><input type="email" name="email" id="email" /></td>
</tr>

<tr>
<td><label>Message</label></td>
<td><textarea name="message" rows="2" cols="16" ></textarea></td>
</tr>

<tr>
<td></td>
<td><input type="submit" id="submit" value="Send" /> </td>
</tr>


</form>

Codeigniter 论坛:http://forum.codeigniter.com/ Codeigniter 2.2.1 用户指南 http://www.codeigniter.com/user_guide/