0

私はいくつかの提案が必要なアプリケーションを開発しています。問題の詳細は次のとおりです。

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

ここでは、検証に失敗したときにページをリロードしようとしている方法を確認できます。ここで問題となるのは、リダイレクト後にのみ使用できるビューフォームにフラッシュデータを表示する必要があり、post変数が失われたためにリダイレクト時に表示されない検証エラーを表示する必要があることです。リダイレクトを使用しない場合、flashdataを表示できませんが、検証エラーのみを表示できます。両方の機能をまとめてほしい。このようにもう一度POStを作成してみました

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

これをformmethodでコメントアウトしました。どうすればこれを実現できますか。

4

2 に答える 2

1

ここに考えがあります。

検証エラー メッセージをフラッシュ データに追加できると思います。このようなものが動作するはずです:

$this->session->set_flashdata('validation_error_messages',validation_errors());

validation_errors 関数の呼び出しに注意してください。これは少し型破りですが、うまくいくはずだと思います。$this->form_validation->run();フォーム検証ライブラリによって検証エラー メッセージが生成されることを確認するために、ステートメントの後にコードが実行されることを確認してください。

于 2012-12-17T09:54:55.923 に答える
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();

問題が発生した場合は、ここに投稿してみてください。

于 2012-12-17T08:29:04.850 に答える