Ya *_* Su 5 php zend-framework flash-message
我有一个表单,提交按钮检查是真还是假.
如果它真的重定向到另一个页面.
如果它是错误的停留在同一页面上并打印错误消息.
使用flash messenger打印出错误消息.但是,在某些情况下,当提交为false时,它不会在第一次尝试时打印,它总是在第二次单击时打印出来.
我做错了什么吗?而且,有没有办法设置差异闪光信使名称?因为,在我的其他具有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();
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我编写的一个动作,演示了您似乎在尝试的内容.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8799 次 |
| 最近记录: |