zend-frameworkzend-formimage-uploadingzend-form-elementajax-upload

Insert custom HTML into Zend_Form


I have a Zend_Form created from a controller like this:

        $form = new Zend_Form;
        $form->setAction('/ad/add')->setMethod('post')->setAttrib('id', 'add_form');
        $form->addElement('text', 'name', array(
            'label' => 'Name',
            'description' => 'Ex.: Samsung Galaxy Tab 10.1',
            'validators' => array(
                'alnum',
                'notEmpty',
                array('stringLength', array('min' => 3, 'max' => 150))
            ),
            'required' => true
        ));
        $form->addElement('textarea', 'description', array(
            'label' => 'Description',
            'description' => 'Make sure you give an accurate description of your item.',
            'validators' => array(
                'alnum',
                'notEmpty',
                array('stringLength', array('min' => 10, 'max' => 255))
            ),
            'required' => true
        ));
        $form->addElement('submit', 'Submit');

When I output that from the view, it works just fine, being rendered with dl and dd magic just as it should.

What I want now is to add an ajax image upload feature to this form. Each image will be uploaded by ajax, displayed in a div, and then a hidden field from my form will be populated with IDs of uploaded files. How can I achieve this, while having the image upload bits INSIDE my forms's markup?


Solution

  • Use view script decorator . I assume you must be uploading image through an iframe . Which you can place it inside the phtml file of your view script decorator.

    Inside your form class do .

    $dec = new Zend_Form_Decorator_ViewScript();
    $dec->setViewScript('Form.phtml');
    
    $this->setDecorators(array($dec,'form'));
    

    Inside Form.phtml to render element do

    <?php echo $this->element->username ;?>
    <?php echo $this->element->image ; ?>
    

    and add your iframe as it is.

    To know more about View script decorator

    http://framework.zend.com/manual/en/zend.form.standardDecorators.html#zend.form.standardDecorators.viewScript