代码之家  ›  专栏  ›  技术社区  ›  overwriter

如何在表单验证后设置要在代码点火器中查看的段?

  •  1
  • overwriter  · 技术社区  · 12 年前

    我正在验证由第三个URL段生成的许多视图上的表单。

    网址: http://example.com/listings/item/12 ( 12将根据项目进行更改 )

    我的两个函数都在下面, 我的问题是,当验证失败时,我如何用第三段刷新页面并显示验证错误?

    附言:我已经尝试过重定向(),但验证错误仍然存在。

    非常感谢。


    这是“项目”功能

        public function item()
    {
    
        if ($this->session->userdata('is_logged_in'))
        {
            $id = $this->uri->segment(3);
            $this->load->model('listings_model');
    
            if ($data['things']= $this->listings_model->get_item($id))
            {
                $this->load->view('list/item_view',$data);
            }
        }
        else
        {
             $this->unauth();
        }
    }
    


    这是我的验证
        public function get_email()
    {
    
        $this->load->library('form_validation');
    
    
            $this->form_validation->set_rules('captcha', 'Captcha', 'required|trim|callback_check_captcha' );
    
    
            if ($this->form_validation->run() == FALSE)
            {
               $this->item();
            }
            else
            {
                $this->load->model('listings_model');
    
                if ($this->listings_model->update_listing())
                {
                    echo "Good";
                }
    
            }
    }
    
    2 回复  |  直到 12 年前
        1
  •  1
  •   Muhammad Nasir    12 年前

    当出现错误时,您可以使用以下代码重定向到您的页面。当出现错误时,您可以将其放在控制器中。

    redirect('listings/item/12/error_message_goes_here', 'location');
    exit;
    

    在您的视图中,检查第四个参数是否存在。如果第四个存在,则显示错误

    if(!empty($this->uri->segment(4))){
    
       $this->output->set_output($this->uri->segment(4));
    
    }
    

    所以你的重点是:你重定向,在重定向中你传递错误消息,然后在视图中,如果存在错误消息,你就会显示它。

        2
  •  0
  •   Subjae Shin    6 年前

    您可以通过设置表单操作属性来设置uri段。

    form_open("controller/method/segments/to/retain", array('method' => 'post'));