更新:已解决 检查当前请求是否是帖子是不够的。这些表格仍然按照创建顺序传递给帮手。如果传递给助手的第一个表单不是发布的表单,则没有足够的验证来防止使用其细节而不是预期的发布表单。写入会话的意外值
新增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