Zend 2.2表单使用flashMessenger提交成功消息?

时间:2013-10-18 07:52:55

标签: php forms zend-framework

我正在尝试使用flashmessenger helper插件进行简单的联系我们页面。

我已经实现了如下,但我的问题是我必须提交表单两次才能让flashmessagers显示在页面中。

我在这里做错了什么..

  1. 在第一次提交时显示消息?
  2. 如何在提交后隐藏表单?
  3. 在我的控制器中

     // Add content to this method:
        public function contactAction()
        {
            $form = new ContactForm();
            $form->get('submit')->setValue('Submit');
    
    
            $request = $this->getRequest();
            if ($request->isPost()) {
                $contact = new Contact();
                $form->setInputFilter($contact->getInputFilter());
                $form->setData($request->getPost());
    
                if ($form->isValid()) {
    
                    $data=$form->getData();
                    //var_dump($data);
    
                    $mail = new Mail\Message();
                    $mail->setBody($data['comment'])
                         ->setFrom($data['email'], $data['name'])
                         ->addTo(IEMAIL, COMPANY )
                         ->addReplyTo($data['email'], $data['name'])
                         ->setSubject($data['subject']);
    
                    $transport = new Mail\Transport\Sendmail();
                    $transport->send($mail);    
                    $this->flashMessenger()->addMessage('Thanks you for the submission ');
                }
                else{
                    $this->flashMessenger()->addMessage('Opps somethinge went wrong ..!');  
                }
            }
            /**/
            return new ViewModel(array(
                'customMessage' => 'Welcome to the sales',
                'form' => $form
            ));
        }
    

    在我看来,我的编码为

    <?php
    $title = 'Contact Us';
    $this->headTitle($title);
    ?>
    <h1><?php echo $this->escapeHtml($title); ?></h1>
    <?php echo $this->customMessage ?>
    <?php
     if ($this->flashMessenger()->hasMessages()): 
    
     echo $this->flashMessenger()->render('success', array('alert', 'alert-danger'));
     // In any of your .phtml files:
     $flash = $this->flashMessenger();
     $flash->setMessageOpenFormat('<div%s>
         <button type="button" class="close" data-dismiss="alert" aria-hidden="true">
             &times;
         </button>
         <ul><li>')
         ->setMessageSeparatorString('</li><li>')
         ->setMessageCloseString('</li></ul></div>');
    
     echo $flash->render('error',   array('alert', 'alert-dismissable', 'alert-danger'));
     echo $flash->render('info',    array('alert', 'alert-dismissable', 'alert-info'));
     echo $flash->render('default', array('alert', 'alert-dismissable', 'alert-warning'));
     echo $flash->render('success', array('alert', 'alert-dismissable', 'alert-success'));
    ?>
    <? endif ?>
    <?php
    $form->setAttribute('action', $this->url('contact', array('action' => 'submit')));
    $form->prepare();
    
    echo $this->form()->openTag($form);
    echo $this->formRow($form->get('name'));
    echo $this->formRow($form->get('email'));
    echo $this->formRow($form->get('subject'));
    echo $this->formRow($form->get('comment'));
    echo $this->formSubmit($form->get('submit'));
    echo $this->form()->closeTag();
    

2 个答案:

答案 0 :(得分:1)

闪光信使不能按照您期望的方式工作。它用于在重定向后向用户显示消息。为此,它会将您传递给它的任何消息添加到会话容器中,以便在下一个请求时检索它们。此行为(Post/Redirect/Get)是Web应用程序中的常见模式,因为它通过刷新页面减少了用户意外重新提交表单提交的可能性。

如果您在$transport->send($mail);之后添加重定向,那么成功的提交应该如您所愿。对于失败的工作,您需要编写自己的帮助程序,同时执行相同的请求消息传递(这是我通常所做的)。相反,您可能希望摆脱失败消息,只是让表单的内置错误消息发送它。

答案 1 :(得分:0)

Change this code in controller...

            $message    = '';
    $flashMessenger = $this->flashMessenger();
    if ($flashMessenger->hasMessages()) {
        $message = $flashMessenger->getMessages();
    }

在视图中

             <?php if(is_array($message) && count($message) > 0){
            foreach($message as $messageKey => $messageValue){?>
            <div style= "color:red"> <?php echo $messageValue; ?> </div>
        <?php } } ?>