在Zend_Form中创建自定义表单元素

时间:2010-01-13 15:23:09

标签: php zend-framework zend-form

我正在尝试在Zend_Form中创建自定义表单字段,以存储SWFUpload所需的HTML代码段(用于Flash文件上传程序)。

我尝试过几个不同的教程,但我感到非常困惑,到目前为止这就是我所拥有的:

/application/forms/elements/SwfUpload.php
                            SwfUploadHelper.php

这些文件在Bootstrap中自动加载(好吧,当然是SwfUpload.php)。

SwfUpload.php:

class Custom_Form_Element_SwfUpload extends Zend_Form_Element
{
    public $helper = 'swfUpload';
}

SwfUploadHelper.php:

class Custom_Form_Helper_SwfUpload extends Zend_View_Helper_FormElement
{
    public function swfUpload()
    {
        $html = '<div id="swfupload-control">
                    <p>Upload upto 5 image files(jpg, png, gif), each having maximum size of 1MB(Use Ctrl/Shift to select multiple files)</p>
                    <input type="button" id="button" />
                    <p id="queuestatus" ></p>
                    <ol id="log"></ol>
                </div>';
        return $html;
    }
}

当我像这样实例化这个类时:

class Form_ApplicationForm extends Zend_Form
{
    public function init()
    {
        $custom = new Custom_Form_Element_SwfUpload('swfupload');
        // etc etc

我收到此错误:

  

消息:插件名称'SwfUpload'   在注册表中找不到;用过的   路径:Zend_View_Helper_:   Zend公司/查看/助手/:/家庭/ mysite的/应用/视图/助理/

是不是希望我的助手在“home/mysite/application/views/helpers/”?我尝试使用文件名“SwfUpload.php”在那里创建相同的帮助程序,但错误仍然存​​在。不确定我这纯粹是文件名/路径问题还是别的什么。

感谢。

2 个答案:

答案 0 :(得分:9)

这就是我最终的结果,希望它可以帮助其他人:

/application/forms/elements/SwfUpload.php

中的

class Custom_Form_Element_SwfUpload extends Zend_Form_Element
{
    public $helper = 'swfUpload'; # maps to method name in SwfUpload helper
    public function init()
    {
        $view = $this->getView();
        $view->addHelperPath(APPLICATION_PATH.'/views/helpers/', 'Custom_Form_Helper');
    }
}
/application/views/helpers/SwfUpload.php

中的

class Custom_Form_Helper_SwfUpload extends Zend_View_Helper_FormElement
{
    public function init(){} 

    public function swfUpload()
    {
        $html = '<div id="swfupload-control">
                    <p>Upload upto 5 image files(jpg, png, gif), each having maximum size of 1MB(Use Ctrl/Shift to select multiple files)</p>
                    <input type="button" id="button" />
                    <p id="queuestatus" ></p>
                    <ol id="log"></ol>
                </div>';
        return $html;
    }
}

答案 1 :(得分:4)

API Docs for Zend_Form_Element

void __construct (string|array|Zend_Config $spec, [ $options = null])

    * string|array|Zend_Config $spec
    * $options

$spec may be:

    * string: name of element
    * array: options with which to configure element
    * Zend_Config: Zend_Config with options for configuring element

    * throws: Zend_Form_Exception if no element name after initialization
    * access: public

看看它扔了什么?尝试

$custom = new Custom_Form_Element_SwfUpload('upload');