【问题标题】:Zend Form ViewScript how to show error messagesZend Form ViewScript 如何显示错误信息
【发布时间】:2012-08-05 10:39:32
【问题描述】:

经过几天的努力,我正在求助于你。我正在使用 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 {

        }       
    }
}

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

有什么想法吗?

【问题讨论】:

    标签: zend-form zend-view zend-decorators


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多