2012-02-16 3 views
0

典型的には、エラーを処理する一般的な方法は、そうようなリダイレクトない簡単である:CakePHPでのエラー処理が、ユーザーの投稿された値をフォームに戻していますか?

if ({something that is error}) { 
    $this->Session->setFlash(__('Error message'), true); 
    $this->redirect(array('controller' => 'some_controller', 'action' => 'some_action')); 
} 

しかし、私は方法のif ($this->request->is('post')) {セクション中に発生する複数のチェックがあります。チェックのいずれかが失敗した場合は、残りの小切手を終了し、以前入力したすべての設定のフォームにユーザーを戻して、フォームにもう一度記入する必要はありません。私はこれをどのように実現するのでしょうか?

public function custom_method() { 
    // get some information here 

    if ($this->request->is('post')) { 
     // do_check 
     if (!do_check) { 
      // set flash 
      // log error 
      // don't run the rest of the checks - go to end of the if ($this->request->is('post')) 
     } 

     // do other check 
     if (!do_other_check) { 
      // set flash 
      // log error 
      // don't run the rest of the checks - go to end of the if ($this->request->is('post')) 
     } 

     // do database update 
    } 

    // do other stuff here 
    // then it goes to render view 
} 

答えて

0

私は私の良き友人(感謝@utoxin)と話をした後、これに対する最適なソリューションを働きました!彼はモデルバリデーションについて言及して私を正しい方向に押し進めてくれました。

public function prepare() { 

    // do some stuff here 

    if ($this->request->is('post')) { 
     $postValid = true; 


     if ($postValid and !some_check) { 
    $this->log('Log error here', 'error'); 
    $this->Model->invalidate('form_field', 'No valid installation file was found for this version.'); 
    $postValid = false; 
     } 

     if($postValid){ 
     // no error, do redirect, continue successfully 
     } 
    } 

    // do other stuff here 
    // show form 
} 
:ここ

は、私はこれを実現する方法であります

関連する問題