更新:已解決 檢查當前請求是否是帖子是不夠的。這些表格仍然按照創建順序傳遞給幫手。如果傳遞給助手的第一個表單不是發佈的表單,則沒有足夠的驗證來防止使用其細節而不是預期的發佈表單。寫入會話的意外值
新增if
在助手附加條款...
if ($postId)
應該已經
if ($postId === $formId)
漂亮的直線前進真的...我只用了2天找到答案。
---原貼下面---
我試圖實現自己的PRG(郵政/重定向/獲取)在ZF1模式。
我使用了一個自定義表單類,它擴展了Zend_Form
來添加一個setUniqueFormId
方法,它基本上是一個隱藏的表單名稱。這些表格連同2個變量($persistData
和$redirectUrl
)一起傳遞給動作助手。 問題是,當我有多個表單時,
第一個
使用從上次調用傳遞給幫助器的值。$persistData
和
$redirectUrl
值總是用於任何後續表單,即使這些表單已更改。
有關爲什麼會出現這種情況的任何想法?任何幫助非常感謝。
更新:我認爲這是使用操作助手的問題。每次調用並傳遞新值時,以前的所有值都會更改。我不太熟悉行動助手經紀人的內幕。任何人都可以擺脫任何光線或提出建議嗎?
---控制器動作---
// Create 2 new forms
$testForm1 = new Application_Form_Test;
$testForm2 = new Application_Form_Test;
// Call a custom function on each form tp create a hidden field called
// "unique_form_id" to help identify the form that has posted the data
$testForm1->setUniqueFormId('test_form_1');
$testForm2->setUniqueFormId('test_form_2');
// Call "Post Redirect Get" Helper and pass a boolean variable for $persistData
$formData1 = $this->_helper->postRedirectGet($testForm1, true);
$formData2 = $this->_helper->postRedirectGet($testForm2, false);
---控制器動作助手---
public function direct($form, $persistData = false, $redirectUrl = null)
{
$formId = $form->getElement('unique_form_id')->getValue();
$currentUrl = implode (
'/',
array (
$this->getRequest()->getModuleName(),
$this->getRequest()->getControllerName(),
$this->getRequest()->getActionName()
)
);
$session = new Zend_Session_Namespace('prg');
$redirectUrl = $redirectUrl ? $redirectUrl : $currentUrl;
if ($this->getRequest()->isPost())
{
$postId = $this->getRequest()->getPost('unique_form_id');
if ($postId)
{
$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector');
$postUrl = $currentUrl;
$session->$postUrl->$postId = array (
'url' => (string) $redirectUrl,
'id' => (string) $postId,
'post' => (array) $this->getRequest()->getPost(),
'persist' => (bool) $persistData
);
Zend_Session::writeClose(true);
$response = $redirector ->setCode(303)
->setExit(true)
->gotoUrl($redirectUrl);
return $response;
}
else {
return false;
}
}
else {
$urlSessionData = $session->$currentUrl;
// Results shown below
Zend_Debug::dump($urlSessionData);
if ($urlSessionData->$formId != null)
{
$formSessionData = $urlSessionData->$formId;
$formPersist = $formSessionData['persist'];
$formPostData = $formSessionData['post'];
if (!$formPersist)
{
unset($urlSessionData->$formId);
}
if(!empty($formPostData))
{
$form->isValid($formPostData);
}
return $formPostData;
}
else {
return false;
}
}
}
---前端控制器插件---
function preDispatch()
{
$session = new Zend_Session_Namespace('prg');
$currentUrl = implode (
'/',
array (
$this->getRequest()->getModuleName(),
$this->getRequest()->getControllerName(),
$this->getRequest()->getActionName()
)
);
// Check if current url is in prg sesison
// If not, we have moved to another URL or its our first visit to the $currentUrl
if ($session->$currentUrl === null)
{
// Remove all prg sessions
Zend_Session::namespaceUnset('prg');
}
return;
}
---轉儲結果---
object(stdClass)#54 (2)
{
["test_form_1"] => array(4)
{
["url"] => string(21) "admin/timeclock/index"
["id"] => string(11) "test_form_1"
["post"] => array(4)
{
["test_element"] => string(0) ""
["submit"] => string(5) "Submit"
["unique_form_id"] => string(11) "test_form_1"
}
["persist"] => bool(false) <-- Expected to be 'true'
}
["test_form_2"] => array(4)
{
["url"] => string(21) "admin/timeclock/index"
["id"] => string(11) "test_form_2"
["post"] => array(4)
{
["test_element"] => string(0) ""
["submit"] => string(5) "Submit"
["unique_form_id"] => string(11) "test_form_2"
}
["persist"] => bool(false) <-- Expected to be 'false'
}
}
你是什麼情況?驗證form1和form2驗證和顯示? 只是驗證form1並顯示?只是驗證form2並顯示?其他? – doydoy44
您可以嘗試將問題縮小到更少的代碼量嗎?您發佈的代碼量可能有問題,我們無法執行測試。 –
我明白爲什麼這總是假的,而不是你如何有2形式。你打2?一個接一個地? – doydoy44