I try to use one Zend_Form for update and create and want to switch both through a param. But my approach using the constructor did not work. When I debug this part the property is set correctly but in the init()-method it is null again.
Maybe I do not understand the Zend_Form approach. Can you help me? Where is the error?
Form Class:
class Application_Form_User extends Zend_Form {
const CREATE = 'create';
const UPDATE = 'update';
protected $_formState;
开发者_如何学运维 public function __construct($options = null, $formState = self::CREATE) {
parent::__construct($options);
$this->_formState = $formState;
$this->setAction('/user/' . $this->_formState);
}
public function init() {
$this->setMethod(Zend_Form::METHOD_POST);
if ($this->_formState == self::CREATE) {
$password = new Zend_Form_Element_Password('password');
$password->setRequired()
->setLabel('Password:')
->addFilter(new Zend_Filter_StringTrim());
}
[..]
Usage in controller action:
$form = new Application_Form_User();
$this->view->form = $form->render();
Thank you.
The init()
method is called inside parent::__construct($options)
. Try this:
public function __construct($options = null, $formState = self::CREATE) {
$this->_formState = $formState;
parent::__construct($options);
$this->setAction('/user/' . $this->_formState);
}
I switched the first two lines of the constructor.
精彩评论