【问题标题】:Zend 2.2 form submit success message using flashMessenger?Zend 2.2表单使用flashMessenger提交成功消息?
【发布时间】:2013-10-18 07:52:55
【问题描述】:

我正在尝试使用 flashmessenger 助手插件做简单的联系我们页面。

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

我在这里做错了什么..

  1. 在第一次提交时显示消息?
  2. 如何在提交后隐藏表单?

在我的控制器中

 // 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();

【问题讨论】:

  • 1.检查您的会话容器framework.zend.com/manual/2.2/en/modules/… 2. 当您拥有有效的发布数据集时,'form' => ''
  • 我认为最后一部分截止,不清楚您所说的“发布数据集'表单'=>'是什么意思?
  • 当你有有效的帖子数据 $form->isValid() 设置 return new ViewModel(array( 'customMessage' => 'Thank you for submit', 'form' => '' )) ;
  • 由于某种原因在逻辑上仍然有问题我已经提交了两次表格以获得正确的信息..不知道为什么
  • 你的代码末尾的冒号是什么? if ($this->flashMessenger()->hasMessages()):

标签: php forms zend-framework


【解决方案1】:

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

如果您在 $transport-&gt;send($mail); 之后添加重定向,那么成功的提交应该会按照您的预期进行。对于失败的工作,您需要编写自己的助手来执行相同的请求消息传递(这是我通常所做的)。相反,您可能希望摆脱失败消息,让表单的内置错误消息发挥作用。

【讨论】:

  • 那么 zend 推荐的方式是提交这个简单的表单,然后显示消息......等等(我对 zend 很陌生)
  • 我已经编辑了我的答案。我建议在传输发送后添加一个重定向,并暂时删除您的“糟糕”消息。
  • 嘿@mahen3d 看看下面的链接link
【解决方案2】:
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 } } ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-17
    • 2013-05-03
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多