簡體   English   中英

Zend Framework / Form Elements:創建自定義類還是?

[英]Zend Framework/Form Elements: Create custom class or?

我想要的結果是連接到WMD (markdown編輯器)的textarea。 並包含div.wmd-preview元素。 我目前在表單中(擴展Zend_Form )...通過裝飾器添加功能

// init()
$this->addElement('textarea', 'bio', array(
    'label' => 'Bio',
    'description' => '<a href="http://daringfireball.net/projects/markdown/syntax" target="_blank" title="get markdown editing/syntax help">Markdown enabled</a>',
    'validators' => array(
        array('StringLength', false, array(0, 1000))
    ),
    'decorators' => array(
        'ViewHelper',
        'Errors',
        array('Description', array('tag' => 'p', 'escape' => false)),
        'Label',
        array('HtmlTag', array('tag' => 'p')),
        new Application_Form_Decorator_WmdPreview,
     )
));
...
// add WMD & Prettify
$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
$view = $bootstrap->getResource('view');
$view->headScript()->prependFile($view->baseUrl("/js/wmd/wmd.js"))
                   ->prependScript('wmd_options = {"output": "Markdown"};')
                   ->prependFile($view->baseUrl("/js/prettify/prettify.js"));
$view->headLink()->prependStylesheet($view->baseUrl('/js/prettify/prettify.css'));
}

但是,如果我希望大多數textarea啟用wmd,我認為將所有這些封裝在單獨的類中可能會更好? 或某種包裝材料? 無論哪種方式,如果我能做些類似的事情會更好

$this->addElement(new Application_Form_Element_WmdEditor); 

或類似的東西

如果答案是創建自定義Zend_Form_Element ,我該怎么做呢? 只是創建一個包含所有標記的自定義視圖助手? 如果我想在文本區域和預覽div之間放置其他裝飾器(例如錯誤和說明)怎么辦?

創建一個擴展Zend_Form_Element_TextArea的新表單元素類:

final class Application_Form_Element_WmdEditor extends Zend_Form_Element_TextArea {
    public $helper = 'WMDEditor';
}

然后像您說一個視圖助手:

final class Application_View_Helper_WMDEditor extends Zend_View_Helper_FormElement {

     public function WMDEditor($name, $value = null, $attribs = null) {
        //set it up here
     }
}

這樣,您還擁有標准表單元素助手的所有優點

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM