2012-08-05 65 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