douzhuangna6906 2015-03-08 06:42
浏览 45
已采纳

验证不适用于HMVC Codeigniter

I am new on HMVC Codeigniter.I would use the codeigniter for form validation on HMVC format of codeigniter but it does not show any effect that means formvalidation not work on my project. But this code should be work on MVC codeigniter. So please help me to solve this problem in my project.I am greatful who help to solve this problem in my project.

** I have an associative controller file feedback.php as below**

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

        }

I have an associative view file feedback.php as below

<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条回答 默认 最新

  • doujiao7483 2015-03-08 08:04
    关注

    In some cases with HMVC you may need to use MY_Form_Validation Library. If also using callbacks in form validation with HMVC will not work unless have code below.

    And if learning best to use codeigniter 2.2.1 version codeigniter 3 coming out but still few bugs in it.

    The other thing to note is you may need to configure your routes in 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";
    

    On form change site url to base_url() base_url with controller name that set in routes.php

    On Your Form

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

    Also why do you need $offset=0 Look into uri segments if need to get ids from url.

    <?php
    
    class MY_Form_validation extends CI_Form_validation {
    
        function run($module = '', $group = '') {
            (is_object($module)) AND $this->CI = &$module;
            return parent::run($group);
        }
    
    } 
    

    And then in the controller would be 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');    
    
    }
    
    }
    
    }
    

    Model

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

    View

    <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 Forum : http://forum.codeigniter.com/ Codeigniter 2.2.1 User Guide http://www.codeigniter.com/user_guide/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大