2016-03-20 51 views
0

這是我我的控制器如何防止在Zend框架中刷新頁面時提交表單?

public function invite() 
{ 
    if($this->getRequest()->isPost()){ 
     $data_array       = $this->getAllParams(); 
     $emailList       = explode(",", $data_array['emails']); 
     $message       = $data_array['message']; 

     $error_emails      = array(); 


     if(sizeof($emailList) <= 1){ 
      $this->view->message  = '<div class="alert alert-danger"> Please enter more than one email address and please separate emails by a "," (comma)</div>';  
     } 
     else{ 
      $x = 0; 
      foreach($emailList as $singleEmail){ 

       if (!filter_var(trim($singleEmail), FILTER_VALIDATE_EMAIL)) { 
        $error_emails[]    = $singleEmail; 
       } 
       else{ 
        //send emails here 
        $x++; 
       } 

      } 

      if(sizeof($error_emails) > 0){ 
       $email_str = implode(",",$error_emails); 
       if($x > 0){ 
        $this->view->message = '<div class="alert alert-success"> Successfully Sent! </div> '; 
       } 
       $this->view->message  .= '<br /><div class="alert alert-danger"> Message to this emails were not sent! <b>'.$email_str.'</b> <br /> Please enter valid emails!</div>';  
      }else{ 
       $this->view->message  = '<div class="alert alert-success"> Successfully Sent! </div>';  
      } 
     } 
     $request = $this->getRequest(); 
     $request->setParam('emails', null); 
     $request->setParam('message', null);    
    } 

} 

我想我找到了解決方案的行動; 正如你所看到的我嘗試setParam方法將值設置爲null。不幸的是,這不起作用。 還未設置陣列沒有工作 - 不知道我是否做得對。 無論如何,我不想重定向。只是想解除。有人可以幫我嗎?我已經嘗試了幾個小時了。提前致謝。

+0

嘗試頭http://php.net/manual/en/function.header.php並強制進行重新驗證。會話/令牌也是另一種選擇。 –

+1

你是什麼原因避免重定向? – max

+0

爲什麼使用html的字符串而不是控制器插件FlashMessenger? – Hooli

回答

2

這個問題不是特定於Zend框架,有(對郵政重定向獲取短)稱爲PRG的模式來處理的形式補交:https://en.wikipedia.org/wiki/Post/Redirect/Get

Zend Framework提供PRG控制器插件http://framework.zend.com/manual/current/en/modules/zend.mvc.plugins.html#post-redirect-get-plugin總之,IT賣場在會話中發佈數據併發出重定向,然後在隨後的獲取請求中返回存儲的發佈數據。

示例代碼,建議您處理表單上GET請求後PRG插件做了重定向:

// Pass in the route/url you want to redirect to after the POST 
$prg = $this->prg('/user/register', true); 

if ($prg instanceof \Zend\Http\PhpEnvironment\Response) { 
    // returned a response to redirect us 
    return $prg; 
} elseif ($prg === false) { 
    // this wasn't a POST request, but there were no params in the flash messenger 
    // probably this is the first time the form was loaded 
    return array('form' => $form); 
} 

// $prg is an array containing the POST params from the previous request 
$form->setData($prg); 

if($form->isValid()) { 
    ... 

但我不同意這種做法,並建議總是處理的POST形式和使用PRG,以顯示與填充數據表格和驗證消息之後(注意$form這裏是Zend\Form一個實例):

if($this->getRequest()->isPost()) { 
    $form->setData($this->getRequest()->fromPost()); 
    if ($form->isValid()) { 
     //handle form 
     // if you don't need to show form again, redirect: 
     return $this->redirect()->toRoute('some/route'); 
    } 
} 

// And now do PRG to re-display form with data and validation errors 
$prg = $this->prg('/user/register', true); 

if ($prg instanceof \Zend\Http\PhpEnvironment\Response) { 
    // returned a response to redirect us 
    return $prg; 
} elseif ($prg === false) { 
    // this wasn't a POST request, but there were no params in the flash messenger 
    // probably this is the first time the form was loaded 
    return array('form' => $form); 
} 

// $prg is an array containing the POST params from the previous request 
$form->setData($prg); 
// make sure form have validation result to display 
$form->isValid(); 
return array('form' => $form); 

但因爲你沒有使用表單,您將需要驗證數據手動兩次。一次處理,一次顯示錯誤消息。如果你不想使用Zend \ Form組件,我會建議看看Zend \ InputFilter來驗證輸入。

0

您可以使用Zend的CSRF保護,即使用令牌,同時提交表單

相關問題