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?

1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

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 to put it in middle of your form simply use viewscipt decorator like I mention above.
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?
It will contain html structure of what you usually put inside <form></form> tag . framework.zend.com/manual/en/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.