Form validation stops working when I render pages using another controller

后端 未结 1 1855
忘掉有多难
忘掉有多难 2021-01-24 05:02

I wrote a quick CI library class to render my pages so I wouldn\'t have to keep typing \'$this->load->view\' all the time and for DRY. Now when I re-render my contact form after

相关标签:
1条回答
  • 2021-01-24 05:19

    Create a view called template.php in your views folder with the following code in it:

    <?php
    
    $this->load->view('fragments/header', $this->_ci_cached_vars); // pass $data vars
    $this->load->view('fragments/navigation');
    $this->load->view($view);
    $this->load->view('fragments/navigation');
    $this->load->view('fragments/footer');
    

    Then use the below controller code:

    public function index() {
    
        $this->form_validation->set_rules('sender_name', 'From', 'required');
        $this->form_validation->set_rules('sender_email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('subject', 'Subject', 'required');
        $this->form_validation->set_rules('message', 'Message', 'required');
    
        if ($this->form_validation->run() === FALSE) {
    
            $data = array(
                'view' => 'contact/contact', 
                'title' => 'Contact Me');
            $this->load->view('template', $data);
        }
    

    You don't need to create a library to do this as you can just create a template view which will load the appropriate views and the specified view and pass the data along to them.

    0 讨论(0)
提交回复
热议问题