Strawberry
Strawberry

Reputation: 67898

Why is this constructor causing a blank output?

class Application_Form_ImageUpload extends Zend_Form
{
    protected $user_id;

    function __construct($id){
            $this->user_id  = $id;
    }
    public function init()
    {
        $user_path = '/var/www/upload/' . $this->user_id;
    ...
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Upload');

        $this->addElement($element, 'image');
        $this->addElement($submit, 'submit');
    }



}

Calling:

        $email = Zend_Auth::getInstance()->getIdentity();
    $user_id = $mapper->getUserId($email);
    $uploadForm = new Application_Form_UserImageUpload($user_id);

After adding the constructor, the output for the form becomes blank. Why is that?

Upvotes: 1

Views: 89

Answers (1)

Fabio
Fabio

Reputation: 19196

Because you've overridden the default ZF constructor which calls init

/**
 * Constructor
 *
 * Registers form view helper as decorator
 *
 * @param mixed $options
 * @return void
 */
public function __construct($options = null)
{
    if (is_array($options)) {
        $this->setOptions($options);
    } elseif ($options instanceof Zend_Config) {
        $this->setConfig($options);
    }

    // Extensions...
    $this->init();

    $this->loadDefaultDecorators();
}

You can solve it by calling it in yours:

function __construct($id){
  $this->user_id  = $id;
  parent::__construct();
}

Or if you want a more generic solution, Zend_Form default constructor calls its setOptions method which uses magic methods to set properties, so if you write a setter in your form class it will be called with options values. Example:

// in your class
class My_Form extends Zend_Form {
  protected $_uid;

  public function setUid($uid) {
    $this->_uid = $uid;
  }

  public function getUid() {
    return $this->_uid;
  }
}
// build the form
$form = new My_Form(array('uid' => 123)); // this calls setUid automagically
echo $form->getUid(); // outputs 123

Upvotes: 2

Related Questions