Constructor parameter not available in init

I am trying to use one Zend_Form to update and create and switch as through a parameter. But my constructor approach didn't work. When I debug this part, the property is set correctly, but in the init () method it is again null.

Perhaps I do not understand the Zend_Form approach. Could you help me? Where is the mistake?

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

Use in action controller:

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

Thank.

+3
source share
1 answer

The method init()is called internally parent::__construct($options). Try the following:

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.

+2
source

Source: https://habr.com/ru/post/1776638/


All Articles