验证不适用于HMVC Codeigniter

时间:2015-03-08 06:42:07

标签: php mysql validation codeigniter

我是HMVC Codeigniter的新手。我会在codeigniter的HMVC格式上使用codeigniter进行表单验证,但它没有显示任何影响,这意味着formvalidation对我的项目不起作用。但是这段代码应该适用于MVC codeigniter。 所以请帮助我在我的项目中解决这个问题。我很高兴有助于在我的项目中解决这个问题。

**我有一个关联控制器文件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>

1 个答案:

答案 0 :(得分:1)

在某些使用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/$1";
$route['feedback/add'] =  "module-folder-name/feedback/add";
$route['feedback/delete'] =  "module-folder-name/feedback/delete";

在表单更改网站网址到base_url() base_url,控制器名称在routes.php中设置

在您的表格上

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

另外,为什么需要$offset=0如果需要从网址获取ID,请查看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/