1

经过几天试图让这个工作,我转向你。我正在使用 ViewScript 装饰器呈现自定义表单,但无法显示错误消息。我尝试了许多不同的解决方案,但 getErrorMessages() 始终为空。

这是我的登录表单:

    public function init() {
    $this->setDecorators(array('Errors', array('ViewScript', array('viewScript' => '_forms/_login.phtml') ) ) );

    $this->setName('loginform');$username = new Zend_Form_Element_Text('username');
    $username->setAttrib('size', '35');
    $username->setRequired(true);
    $username->addValidator('NotEmpty');
    $username->setDecorators( array('ViewHelper',  'Errors') );

    $password = new Zend_Form_Element_Password('password');
    $password->setRequired(true);
    $password->addValidator('NotEmpty');
    $password->setDecorators( array('ViewHelper',  'Errors') );

    $submit = new Zend_Form_Element_Button('submit');
    $submit->setAttrib('type', 'submit');
    $submit->setDecorators( array('ViewHelper',  'Errors') );

    $this->setMethod('post'); 
    $this->setAction('/auth/index');

    $this->addElements(array($username, $password, $submit));
}
}

这是我的观点:

<form action="<?php echo $this->element->getAction() ?>" name="<?php echo $this->element->getName() ?>"  method="<?php echo $this->element->getMethod() ?>" id="login-form">
<ul class="nav">
<li><?php echo $this->element->username; ?></li>
<li><?php echo $this->element->password; ?></li>
<li><?php echo $this->element->submit; ?></li>
</ul>

这是我的处理动作:

 public function indexAction()
{
    $request = $this->getRequest();
    $form = new Form_LoginForm();
    if ($request->isPost()){
        if($form->isValid( $this->_request->getPost() )) {  //This part works

        }
        else {

        }       
    }
}

尝试了此处看到的示例,但没有成功。

有任何想法吗?

4

1 回答 1

3

试试这个,你可以把错误信息放在你想打印的任何地方。

<li><?php echo $this->element->username .
 $this->formErrors($this->element->username->getMessages() ); ?></li> 
<li><?php echo $this->element->password. 
$this->formErrors($this->element->password->getMessages() ); ?></li> 
<li><?php echo $this->element->submit; ?></li>
于 2013-02-27T22:43:58.600 回答