2

我需要在我的 Zend Framework 应用程序中创建一些自定义表单元素(使用自定义视图助手)。问题是它们都非常相似。我想创建一个基本视图助手类,每个都可以扩展,并实现我需要的抽象函数。

解决方案:

因此,如果我的Picker元素是抽象类并且ContactPickerOrganizationPicker扩展类......

表单元素:

class My_Form_Element_ContactPicker extends My_Form_Element_Picker
{
    /**
     * Default form view helper to use for rendering
     * @var string
     */
    public $helper = "contactPickerElement";
}

视图助手:

class My_View_Helper_ContactPickerElement extends My_View_Helper_PickerElement
{
    public function contactPickerElement($name, $value = null, $attribs = null)
    {
        // I don't need to do anything in this function.
        // I only need the parent to do all the work.
        return parent::pickerElement($name, $value, $attribs);
    }

    protected function myAbstractFunctionThatMustBeImplemented()
    {
        // This function will do all the work specific to this extending class.
        $model = new ContactModel();
        return $model->foobar;
    }
}

这是抽象视图助手:

abstract class Evanta_View_Helper_PickerElement extends Zend_View_Helper_FormElement
{
   /**
    * This function would have been called automatically, but since it's being extended...
    * Any extending classes must remember to manually call this function 
    */
   public function modalPickerElement($name, $value = null, $attribs = null)
    {
        $html = 'My picker element HTML';
        return $html;
    }
    
    /**
     * This function must be implemented by any extending classes
     */
    abstract protected function myAbstractFunctionThatMustBeImplemented();
}
4

1 回答 1

4

在您的某个地方,您Bootstrap需要定义表单资源的去向,我建议使用自动加载器:

$autoloader->addResourceType('default_form', 'forms/', 'Form');

我加载自动加载器的整个方法看起来像这样

protected function _initAutoload()
{
    $autoloader = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'Default_',
            'basePath'  => $this->_root,
        ));

    // Here a define a resource named 'default_form', this can be anything,
    // 2nd param is the path relative to my application folder,
    // and 3rd param is the prefix for the classes inside that folder
    $autoloader->addResourceType('default_form', 'forms/', 'Form');

    Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);

    return $autoloader;
}

然后,您可以在此forms文件夹中创建一个名为的文件夹,然后创建一个Element名为并定义类的文件Default_Form_Element_Picker.php,您可以覆盖诸如isValid()setValues()getValue()loadDefaultDecorators()__construct()

Default_Form_Element_Picker extends Zend_Form_Element_Xhtml {

}        

您可以扩展此类以创建其他表单元素,方法是创建具有名称Default_Form_Element_ContactPicker.phpDefault_Form_Element_OrganizationPicker.php

Default_Form_Element_ContactPicker extends Default_Form_Element_Picker {

}

Default_Form_Element_OrganizationPicker extends Default_Form_Element_Picker {

}

您也可以在这里添加一个用于创建表单的类,称之为Default_Form_A.php

Default_Form_A extends Zend_Form {

    public function __construct()
    {
        // create form elements here suing Zend_Form_Element and your new Custom Elements
    }
}

文件结构

 application 
 |--forms   
 |----Default_Form_A.php
 |----Element
 |------ Default_Form_Element_Picker.php
 |------ Default_Form_Element_ContactPicker.php
 |------ Default_Form_Element_OrganizationPicker.php
于 2010-09-22T07:21:37.237 回答