zend framework 1 Form - Note element lost its value if there is validation error

时间:2015-04-23 05:39:25

标签: zend-framework zend-form zend-form-element

I have a Note element in my zend framework 1 form used for registration. It is defined in the format:

$captcha_reload =   new Zend_Form_Element_Note('captcha_reload',  
                                array('value'=>"<div id='captcha_reload_div'>
                                                <a href='javascript:return false;' 
                            id='change-image'>Change text.</a></div>"));        
$this->addElement($captcha_reload);                      

This element displays a hyperlink and displays perfectly during registration page call.

The problem is during form submission. This note element doesn't displays anything (ie missing the hyperlink) if there is form validation error.

I have checked and tried the code below:

$this->setDefaults(array('captcha_reload'=>"<div id='captcha_reload_div'>
                                                <a href='javascript:return false;' 
                            id='change-image'>Change text.</a></div>")); 

But still there is no value if there is form validation error.

For Note element, I have included the following in the Zend Registration Form page:

class Zend_Form_Element_Note extends Zend_Form_Element_Xhtml  
{  
   public $helper = 'formNote';  
}

2 个答案:

答案 0 :(得分:2)

提交表单时,它会覆盖元素的value属性。由于没有提交任何内容,当表单再次echo以显示表单错误时,元素的值也没有。

也许在元素中添加isValid函数?

// pseudo-code
public function isValid($value, $context = null) {
  $this->_setValue("<div id='captcha_reload_div'><a href='javascript:return false;' id='change-image'>Change text.</a></div>");
  return true;
}

这会将值重置为自定义文本,并在不进行任何检查的情况下返回true(因为您知道值是您想要的值)。随后,当表单echo再次显示时,它将显示isValid

中设置的值

答案 1 :(得分:1)

class Zend_Form_Element_Note extends Zend_Form_Element_Xhtml  
{  
   public $helper = 'formNote';  
   public function isValid($value, $context = null)
   {
      return true;
   }
}

我已将isValid()添加到Note类中,它可以正常工作。它不需要在Note类中使用_setValue()。

相关问题