2012-08-05 64 views
1

后试图得到这个工作,我转向你天。我使用ViewScript装饰器渲染自定义窗体,无法显示错误消息。我尝试了许多不同的解决方案,但是getErrorMessages()始终是空的。Zend的形式ViewScript如何显示错误消息

这是我的登录表单:

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)); 
} 
} 

这是我viewscript:

<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 { 

     }  
    } 
} 

试图看到here没有成功的例子。

任何想法?

回答

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> 
+0

谢谢你这个awsome解决方案,工作很棒。 – mboullouz 2013-07-06 14:22:27