0

我有一个从这样的控制器创建的 Zend_Form:

        $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');

当我从视图中输出它时,它工作得很好,按照它应该的方式用 dl 和 dd 魔法渲染。

我现在想要的是在这个表单中添加一个 ajax 图片上传功能。每个图像都将通过 ajax 上传,显示在一个 div 中,然后我的表单中的一个隐藏字段将填充上传文件的 ID。我怎样才能做到这一点,同时将图像上传位放在我的表单标记中?

4

1 回答 1

2

使用视图脚本装饰器。我假设您必须通过 iframe 上传图片。您可以将它放在视图脚本装饰器的 phtml 文件中。

在你的表单类里面做 .

$dec = new Zend_Form_Decorator_ViewScript();
$dec->setViewScript('Form.phtml');

$this->setDecorators(array($dec,'form'));

在 Form.phtml 中渲染元素

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

并按原样添加您的 iframe。

了解更多关于 View 脚本装饰器

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

于 2012-06-27T03:39:34.240 回答