Php Zend 2.2表单使用flashMessenger提交成功消息?
我正在尝试用flashmessenger助手插件做一个简单的联系我们页面 我已经按如下方式实现了它,但我的问题是我必须提交两次表单才能让FlashMessager显示在页面中 我在这里做错了什么Php Zend 2.2表单使用flashMessenger提交成功消息?,php,forms,zend-framework,Php,Forms,Zend Framework,我正在尝试用flashmessenger助手插件做一个简单的联系我们页面 我已经按如下方式实现了它,但我的问题是我必须提交两次表单才能让FlashMessager显示在页面中 我在这里做错了什么 在第一次提交时显示消息 提交后如何隐藏表单 在我的控制器中 // Add content to this method: public function contactAction() { $form = new ContactForm(); $form
// 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">
×
</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();
flash messenger无法按您期望的方式工作。它设计用于在重定向后向用户显示消息。为此,它将您传递给它的任何消息添加到会话容器中,以便在下一个请求中检索它们。此行为()是web应用程序中的常见模式,因为它通过刷新页面减少了用户意外重新提交表单提交的机会
如果在$transport->send($mail)之后添加重定向代码>,则成功提交的内容应符合您的预期。对于失败的请求,您需要编写自己的助手来执行相同的请求消息传递(这是我通常做的)。相反,您可能希望摆脱失败消息,让表单的内置错误消息完成它的工作
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 } } ?>
1。检查会话容器2。当您拥有有效的post数据集时,$form'=>''我想最后一部分已经结束,不清楚您所说的“post数据集'form'=>''是什么意思吗?当您拥有有效的post数据时,$form->isValid()设置返回新的视图模型(数组('customMessage'=>'谢谢提交','form'=>'');由于某些原因,逻辑上仍然有问题。我已经提交了两次表单以获得正确的消息。不确定为什么代码末尾的冒号是什么?如果($this->flashMessenger()->hasMessages()):那么zend推荐的这种简单表单提交然后显示消息的方法是什么…等等(我对zend很陌生)我已经编辑了我的答案。我建议在传输发送后添加一个重定向,现在只删除你的“Oops”消息。嘿@mahen3d看看下面的链接