0

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?

Zorrocaesar
  • 748
  • 1
  • 7
  • 22
  • 2
    Possible duplicate of [Add some html to Zend Forms](http://stackoverflow.com/questions/2566432/add-some-html-to-zend-forms) – Mogsdad Feb 25 '16 at 19:55

1 Answers1

2

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

Mr Coder
  • 8,169
  • 5
  • 45
  • 74
  • Actually I will upload the image using one of the jQuery plugins available out there. What I need to have is the container for showing uploaded images and the input field for the upload plugin to pick up and use. And I want this in the middle of my form. – Zorrocaesar Jun 27 '12 at 10:51
  • @Zorrocaesar to put it in middle of your form simply use viewscipt decorator like I mention above. – Mr Coder Jun 27 '12 at 10:52
  • than it seams I didn't understand your answer. What should Form.phtml contain? The main form, or the new decorator markup? Look at my example code? How should I integrate your answer with my code? – Zorrocaesar Jun 27 '12 at 12:07
  • It will contain html structure of what you usually put inside
    tag . http://framework.zend.com/manual/en/zend.form.standardDecorators.html#zend.form.standardDecorators.viewScript
    – Mr Coder Jun 27 '12 at 15:07
  • Also see http://stackoverflow.com/questions/1277849/zend-framework-forms-decorators-and-validation-should-i-go-back-to-plain-html/1278880#1278880 – Cal Jacobson Jul 03 '12 at 19:58