Zend フォームで検証が失敗した後にビューを再表示する際に問題があります。
私の最初のアクションは次のようになります
public function test1Action() {
// get a form and pass it to the view
$this->view->form = $this->getForm();
// extra stuff I need to display
$this->view->name = "Bob";
}
とビュー
Hello <?= $this->name?>
<?php echo $this->form;?>
問題は、フォームが送信された後に呼び出されるアクション「test2」に検証エラーがある場合に発生します。
public function test2Action() {
if (!$this->getRequest()->isPost()) {
return $this->_forward('test1');
}
$form = $this->getForm();
if (!$form->isValid($_POST)) {
$this->view->form = $form;
return $this->render('test1'); // go back to test1
}
}
実際、変数「name」が失われ、ビューが正しくありません。「Hello Bob」と言う代わりに、「Hello」と言います。
それをどのように処理すればよいですか?test1 を再度レンダリングするのではなく、test1 にリダイレクトする必要がありますか? どうやって ?
編集
コーダー氏の答えに続いて、私が最終的に得たものは次のとおりです。
コントローラ:
public function getForm() {
// what is important is that the form goes to action test3 and not test4
// SNIP form creation with a field username
return $form;
}
public function test3Action()
{
$this->view->form = $this->getForm();
$this->view->name = "Bob";
if(!$this->getRequest()->isPost())return;
if($this->view->form->isValid($_POST))
{
//save the data and redirect
$values = $this->view->form->getValues();
$username = $values["username"];
$defaultSession = new Zend_Session_Namespace('asdf');
$defaultSession->username = $username;
$this->_helper->redirector('test4');
}
}
public function test4Action()
{
$defaultSession = new Zend_Session_Namespace('asdf');
$this->view->username = $defaultSession->username;
}
test3.phtml
Hello <?= $this->name?>
<?php echo $this->form;?>
test4.phtml
success for <?php echo $this->username;?>