【问题标题】:creating a custom form element in Zend_Form在 Zend_Form 中创建自定义表单元素
【发布时间】:2011-09-01 13:52:04
【问题描述】:

我正在尝试在Zend_Form 中创建一个自定义表单字段来存储 SWFUpload 所需的 HTML 的 sn-p(用于 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/View/Helper/:/home/mysite/application/views/helpers/

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

谢谢。

【问题讨论】:

  • 这里有一个像你这样的问题:stackoverflow.com/questions/2056737/…
  • 这实际上也是我的问题,不打算发送垃圾邮件,但当问题类型发生变化时,它在 stackoverflow 上很困难。这更像是理论的案例,这更像是故障排除细节的案例。

标签: php zend-framework zend-form


【解决方案1】:

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

/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;
    }
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 刚刚注意到我自己,天哪。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多