5

我有一个表单和提交按钮检查是真还是假。

如果是真的重定向到另一个页面。

如果它是错误的,请留在同一页面上并打印错误消息。

错误消息使用 Flash 信使打印出来。但是,在某些情况下,当提交为假时,它不会在第一次尝试中打印,它总是在第二次点击时打印出来。

我做错了什么吗?还有,有没有办法设置不同的闪光信使名称?因为,在我的其他有 flash messenger 的页面上,刷新页面时会打印出错误。

这是代码:

if(isset($_POST['submit'])) {
    // code to inputfields

    if(true) {
        //redirect to some page
    } else {
        // print the flash error on the same page
        $this->_helper->flashMessenger->addMessage(" This email is already taken");
        $this->view->messages = $this->_helper->flashMessenger->getMessages();
    }
}

HTML:

<center>
            <div style="color:red">
            <?php if (count($this->messages)) : ?>
                <?php foreach ($this->messages as $message) : ?>
                <div id="field_name">
                <strong style="text-transform:capitalize;">Email </strong>
                    - <?php echo $this->escape($message); ?>
                </div>
                <?php endforeach; ?>
            <?php endif; ?>
            </div>
        </center>
4

1 回答 1

9

flashmessenger 确实设计用于某种重定向,因此您看到的任何消息都可能来自先前操作的执行。您当前的代码在第一个“帖子”期间不会闪烁任何消息。

如果您尝试以下操作,您可能会有一些运气:

public function init()
{
    //This will catch any messege set in any action in this controller and send
    //it to the view on the next request.
    if ($this->_helper->FlashMessenger->hasMessages()) {
        $this->view->messages = $this->_helper->FlashMessenger->getMessages();
    }
}

public function someAction()
{
    if(isset($_POST['submit'])) {
    // code to inputfields
        if(true) {
        //redirect to some page
        } else {
            // print the flash error on the same page
            $this->_helper->flashMessenger->addMessage(" This email is already taken");
            //will redirect back to original url. May help, may not
            $this->_redirect($this->getRequest()->getRequestUri());
        }
    }
}

这是我编写的一个动作,它展示了您似乎正在尝试的内容。

public function updatetrackAction()
    {
        //get the page number
        $session = new Zend_Session_Namespace('page');
        $id = $this->getRequest()->getParam('id');
        //get the entity object
        $model = new Music_Model_Mapper_Track();
        $track = $model->findById($id);
        //get the form
        $form = new Admin_Form_Track();
        $form->setAction('/admin/music/updatetrack/');
        //test for 'post' 'valid' and update info
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $data = $form->getValues();

                $newTrack = new Music_Model_Track($data);

                $update = $model->saveTrack($newTrack);
                //add message
                $this->message->addMessage("Update of track '$update->title' complete!");
                //redirects back to the same page number the request came from
                $this->getHelper('Redirector')->gotoSimple('update', null, null, array('page' => $session->page));
            }
        } else {
            //if not post display current information
            //populate() only accepts an array - no objects -
            $form->populate($track->toArray());
            $this->view->form = $form;
        }
    }
于 2013-02-02T07:59:38.150 回答