Codeigniter中的重定向,POST和Flashdata问题

时间:2012-12-17 08:04:33

标签: codeigniter validation message

我正在开发一个我需要一些建议的应用程序。以下是问题的详细信息。

public function form()
{
    $this->load->helper('inflector');
    $id =   $this->uri->segment(3,0); 

    if($data = $this->input->post()){
        $result =   $this->form_validation->run();
        if($result){
            if($id > 0){
              // here update code
            }else{
                $this->mymodel->insert($data);
                $this->session->set_flashdata('message','The page has been added successfully.');
                $this->redirect =   "mycontroller/index";
                $this->view     =   FALSE;
            }
        }else{
            //$this->call_post($data);
            $this->session->set_flashdata('message','The Red fields are required');
            $this->view     =   FALSE;
            $this->redirect =   "mycontroller/form/$id";
        }
    }else{

        $row    =   $this->mymodel->fetch_row($id);
        $this->data[]=  $row;
    }
}


public function _remap($method, $parameters)
{
    if (method_exists($this, $method))
    {
        $return =   call_user_func_array(array($this, $method),$parameters);
    }else{
        show_404();
    }

    if(strlen($this->view) > 0)
    {
        $this->template->build('default',array());

    }else{
        redirect($this->redirect);
    }   
}   

在这里,您可以看到我如何尝试在验证失败时重新加载页面。 现在的问题是我必须在视图表单上显示flash数据,该数据仅在重定向后可用,并且我需要显示由于丢失post变量而未在重定向上显示的验证错误。如果我不使用重定向然后不能显示flashdata但只有验证错误。我希望这两个功能都能收集起来。我甚至试过像这样再次创建POSt

public function call_post($data)
{
    foreach($data as $key => $row){
        $_POST[$key]    =   $row;
    }   
}

我在形式方法中评论过。我怎样才能做到这一点。

2 个答案:

答案 0 :(得分:1)

这是一个想法。

我认为您可以将验证错误消息添加到Flash数据中。这样的事情应该有效:

  

$这 - >会话而> set_flashdata( 'validation_error_messages',VALIDATION_ERRORS());

注意对validation_errors函数的调用。这有点不合常规,但我认为它应该有效。只需确保在语句$this->form_validation->run();之后执行代码,以确保表单验证库生成验证错误消息。

答案 1 :(得分:0)

嗯,我有一点不同的方法,希望能在这里帮助你

   mycontroller extend CI_Controller{

      function _remap($method,$params){
         switch($method){
            case 'form':
                   $this->form($params);
            break;
            default:
                    $this->index();
            break;
         }
      }

      function form(){
          $this->load->helper('inflector');
          $id =   $this->uri->segment(3,0); 

          $this->form_validation->set_rules('name','Named','required|trim');
          $this->form_validation->set_rules('email','email','required|valid_email|trim');

          // if validation fails and also for first time form called
          if(!$this->form_validation->run()){
                   $this->template->build('default',array());
           }
           else{ // validation passed
               $this->save($id)
           }

      }

       function save($id = 0){
           $data = $this->input->post();
          if($id == 0){
            $this->mymodel->insert($data);
            $this->session->set_flashdata('message','The page has been added successfully.');
            $this->redirect =   "mycontroller/index";
            $this->view     =   FALSE;
        }else{
            // update the field
            $this->session->set_flashdata('message','The Red fields are required');
        }

        redirect("mycontroller/index");

       }

   }

您的表单/默认视图应该是这样的

<?
    if(validation_errors())
      echo validation_errors();
    elseif($this->session->flashdata('message'))
       echo $this->session->flashdata('message'); 

    echo form_open(uri_string());// post data to same url
    echo form_input('name',set_value('name'));
     echo form_input('email',set_value('email'));
     echo form_submit('submit');
     echo form_close();

如果您在此处遇到任何问题,请尝试。

相关问题