如何使用zend框架制作自定义图像文件表单

时间:2011-06-12 14:44:14

标签: php zend-framework zend-form-element

如何通过扩展zend表单元素来制作自定义图像文件? html中的输出如下所示:

<div class="image-file-wrapper">
     <img src="yourimagepath" />
     <br />
     <input id="image-file" name="image-file" type="file" />
</div>

更新

我的自定义表单元素

class Engine_Form_Element_ImageFile extends Zend_Form_Element_File {
public $helper = 'FormImageFile';

protected $_image;

public function setImage($src) {
    $this->_image = $src;

    return $this;    
}

public function loadDefaultDecorators() {
    if($this->loadDefaultDecoratorsIsDisabled()) {
        return;
    }

    $decorators = $this->getDecorators();

    if(empty($decorators)) {
        $this->addDecorator('ViewHelper');
        Engine_Form::addDefaultDecorators($this);
    }
}
}

我的自定义视图助手:

class Engine_View_Helper_FormImageFile extends Zend_View_Helper_FormElement {
public function formImageFile($name, $value = null, $attribs = null, $options = null) {
    return '<div class="image-file-wrapper"><img src="test.png" /><input type="file" /></div>';    
}    
}

当我尝试添加它时,

$this->addElement('ImageFile', 'imageFile', array('label' => 'Test Image'));

发生错误:

Warning: Exception caught by form: No file decorator found... unable to render file element Stack Trace: #0 C:\xampp\htdocs\NEOBBS_v6\library\Zend\Form\Decorator\FormElements.php(101): Zend_Form_Element_File->render() #1 C:\xampp\htdocs\NEOBBS_v6\library\Zend\Form.php(2904): Zend_Form_Decorator_FormElements->render('') #2 C:\xampp\htdocs\NEOBBS_v6\library\Zend\Form.php(2920): Zend_Form->render() #3 C:\xampp\htdocs\NEOBBS_v6\application\themes\admin\settings.phtml(8): Zend_Form->__toString() #4 C:\xampp\htdocs\NEOBBS_v6\library\Zend\View.php(108): include('C:\xampp\htdocs...') #5 C:\xampp\htdocs\NEOBBS_v6\library\Zend\View\Abstract.php(880): Zend_View->_run('C:\xampp\htdocs...') #6 C:\xampp\htdocs\NEOBBS_v6\library\Zend\Controller\Action\Helper\ViewRenderer.php(897): Zend_View_Abstract->render('settings.phtml') #7 C:\xampp\htdocs\NEOBBS_v6\library\Zend\Controller\Action.php(243): Zend_Controller_Action_Helper_ViewRenderer->renderScript('settings.phtml', NULL) #8 C:\xampp\htdocs\NEOBBS_v6\application\modules\admin\contr in C:\xampp\htdocs\NEOBBS_v6\library\Zend\Form.php on line 2925

我该如何解决?

提前致谢,

布赖恩

1 个答案:

答案 0 :(得分:0)