【问题标题】:Magento display message on pageMagento 在页面上显示消息
【发布时间】:2014-04-09 09:39:29
【问题描述】:

我开始在 magento 上运气好几个星期了。此后的挑战。我试图在我的输入框上方显示消息但无济于事。下面是我的 phtml 和控制器。

密码.phtml

<div id="account-profile">
<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
<form action="<?php echo Mage::getUrl('customer/account/emailopt'); ?>" method="post">
    <input name="form_key" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" />
    <div class="fieldset">
        <ul class="form-list">
            <li class="fields">
                <div class="field">
                    <div class="input-box">
                        <label for="email"><?php echo $this->__('Email') ?><span class="required">*</span></label>
                        <input name="email" id="email" title="Email" value="" class="required-entry input-text validate-email" type="text" />
                    </div>
                </div>
            </li>        
        </ul>
    </div>
    <button type="submit" class="btn-dbbdr" title="<?php echo $this->__('Edit Options') ?>"><span><span><?php echo $this->__('Edit Options') ?></span></span></button>
</form>

accountController.php

    public function emailoptAction()
    { 
    if (!$this->_validateFormKey()) { 
        return $this->_redirect('*/emailoptions');
    }
        $customer = Mage::getSingleton('customer/session')->getCustomer();
        $email = $customer->getEmail();        
        $_email =$this->getRequest()->getParam('email');
        if ($email != $_email) {
          echo 'This is not your valid email';
        } else {
          echo 'proceed here'; 
        }
    $this->_redirect('*/emailoptions');

    } 

这里发生的事情是每次我单击提交时,它只是回显消息,然后再次重定向到该页面。

【问题讨论】:

    标签: validation magento email post input


    【解决方案1】:

    您在回显后重定向可能不起作用。

    使用下面的代码进行消息显示。

    $message = $this->__('Error: This is not your valid email');
    Mage::getSingleton('core/session')->addError($message);
    

    更多信息here

    更新

     public function emailoptAction()
            { 
            if (!$this->_validateFormKey()) { 
                return $this->_redirect('*/emailoptions');
            }
                $customer = Mage::getSingleton('customer/session')->getCustomer();
                $email = $customer->getEmail();        
                $_email =$this->getRequest()->getParam('email');
                if ($email != $_email) {
                  //echo 'This is not your valid email';
                  $message = $this->__('This is not your valid email');
                  Mage::getSingleton('core/session')->addError($message);
    
                } else {
                  //echo 'proceed here'; 
                  $message = $this->__('proceed here');
                  Mage::getSingleton('core/session')->addSuccess($message);
                }
            $this->_redirect('*/emailoptions');
    
            } 
    

    【讨论】:

    • getMessagesBlock()->getGroupedHtml() ?> 显示的消息作为 session 中存储的消息
    • 只需要在redirect之前加上上面的代码,去掉echo
    • 现在正在显示。只是它没有显示在 my.account.wrapper 下的 emailoptions.phtml 中我想要的位置。它显示在页面的最顶部。
    • 消息一旦显示将从会话中删除,因此您需要将 getMessagesBlock()->getGroupedHtml() ?> 代码放在适当的位置
    • 我在提交按钮下方。
    【解决方案2】:

    如果您在 admin panel 工作,请使用此

    Mage::getSingleton('adminhtml/session')-&gt;addError("Error here");

    否则将其用于frontend

    Mage::getSingleton('core/session')-&gt;addError("Error here");

    对于checkout

    Mage::getSingleton('checkout/session')-&gt;addError("Error here");

    在您的controller 中使用这些代码。

    我觉得很简单

    if ($email != $_email) {
              Mage::getSingleton('adminhtml/session')->addError("Not valid"); //change adminhtml as per need
              $this->_redirect('your page');
            } else {
              Mage::getSingleton('adminhtml/session')->addSuccess("Valid");
              $this->_redirect('your page');
            }
    

    【讨论】:

    • 消息已显示。我希望它在控制器验证电子邮件是否有效后显示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多