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/

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

报告相同问题?

悬赏问题

  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败