2

Zend Framework アプリケーションで (カスタム ビュー ヘルパーを使用して) いくつかのカスタム フォーム要素を作成する必要があります。問題は、それらがそれぞれ非常に似ていることです。これらのそれぞれを拡張できる基本ビュー ヘルパー クラスを作成し、必要な抽象関数を実装したいと考えています。

解決:

したがって、私のPicker要素が抽象クラスでContactPickerありOrganizationPicker、拡張クラスである場合...

フォーム要素:

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 に答える