如何访问和创建错误消息

时间:2015-10-14 05:01:35

标签: php magento

我有一个登录表单。如果我在没有输入任何内容的情况下提交它,则输入字段上应该出现错误消息。

如何从magento控制器访问错误消息?我该如何编写消息?

<form action="<?php echo $this->getPostActionUrl() ?>" method="post">
        <?php echo $this->getBlockHtml('formkey'); ?>
        <div class="block-content">
    <?php echo $this->__('Save your Designs');?><br>&nbsp;<br>
            <div class="col-reg registered-account">
                <div class="email-input">
        <label for="mini-login" class="required"><em>*</em><?php echo $this->__('Email Address') ?></label>
                    <input title="<?php echo $this->__('User Name')?>" type="text" name="login[username]" id="mini-login" class="required-entry input-text" value="<?php echo $this->__('User Name')?>" onFocus="if(this.value != '') {this.value = '';}" onBlur="if (this.value == '') {this.value = '<?php echo $this->__('User Name')?>';}" />
                </div>
                <div class="pass-input">
        <label for="mini-password" class="required"><em>*</em>test<?php echo $this->__('Password') ?></label>
                    <input title="<?php echo $this->__('Password')?>" type="password"  name="login[password]" id="mini-password" class="input-text required-entry" value="........." onFocus="if(this.value != '') {this.value = '';}" onBlur="if (this.value == '') {this.value = '.........';}"  />
                </div>
                <!--<div class="ft-link-p">
                    <a title="<?php echo $this->__('Forgot your password?')?>" href="<?php echo $this->getForgotPasswordUrl() ?>" class="f-left" target="_blank"><?php echo $this->__('Forgot your password?') ?></a>   
                </div>-->
                <div class="actions">
                    <div class="submit-login">
                        <input title="<?php echo $this->__('Login')?>" type="submit" class="button btn-submit-login" name="submit" value="<?php echo $this->__('LOGIN') ?>" />
                    </div>  
                </div>

            </div>
            <div class="col-reg login-customer">
                <h2><?php echo $this->__('Registration is free and easy!');?></h2>
                <br>
                <ul class="list-log">
                    <li><?php echo $this->__('Faster checkout');?></li>
                    <li><?php echo $this->__('save your own designs');?></li>
                    <li><?php echo $this->__('View and track orders and more');?></li>
                </ul>
                <a class="btn-reg-popup" title="<?php echo $this->__('Register')?>" href="<?php echo $this->getUrl('customer/account/create')?>"><?php echo $this->__('Create an account');?></a>
            </div>
            <div style="clear:both;"></div>
        </div>
    </form>

1 个答案:

答案 0 :(得分:1)

在phtml文件中包含以下脚本

<script type="text/javascript">
//<![CDATA[
var Form = new VarienForm('formid', true);
//]]>
</script>
to access in controller use

if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) {
    $error = true;
}
if ($error) {
    throw new Exception();
}