构造函数参数在init中不可用

时间:2010-11-24 08:14:09

标签: php zend-framework zend-form

我尝试使用一个Zend_Form进行更新并创建并希望通过参数切换。但是我使用构造函数的方法不起作用。当我调试这个部分时,属性设置正确但在init() - 方法中它再次为null。

也许我不理解Zend_Form方法。你能帮助我吗?错误在哪里?

表单类:

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());
    }
[..]

控制器操作的用法:

$form = new Application_Form_User();
$this->view->form = $form->render();

谢谢。

1 个答案:

答案 0 :(得分:2)

init()内调用parent::__construct($options)方法。试试这个:

public function __construct($options = null, $formState = self::CREATE) {
    $this->_formState = $formState;
    parent::__construct($options);

    $this->setAction('/user/' . $this->_formState);
}

我切换了构造函数的前两行。