2008-12-17 134 views
4

我有一個擴展Zend_Form的這樣一類(簡體):Zend框架 - Zend_Form的裝飾問題

class Core_Form extends Zend_Form 
{ 
    protected static $_elementDecorators = array(
     'ViewHelper', 
     'Errors', 
     array('Label'), 
     array('HtmlTag', array('tag' => 'li')), 
    ); 

    public function loadDefaultDecorators() 
    { 
     $this->setElementDecorators(self::$_elementDecorators); 
    } 
} 

然後我用這個類來創建我的所有形式:

class ExampleForm extends Core_Form 
{ 
    public function init() 
    { 
     // Example Field 
     $example = new Zend_Form_Element_Hidden('example'); 
     $this->addElement($example); 
    } 
} 

在一個我的意見,我有一個需要顯示只有這一個字段(沒有任何其他生成的Zend_Form)。所以,在我看來,我有這樣的:

<?php echo $this->exampleForm->example; ?> 

這工作得很好,除了它會產生這樣的領域:

<li><input type="hidden" name="example" value=""></li> 

這顯然是因爲我設置元素的裝飾,包括HtmlTag:標籤= >'李'。

我的問題是:如何禁用此元素的所有裝飾器。我不需要裝飾器來隱藏輸入元素。

回答

4

最好的地方,將其設置爲公共職能loadDefaultDecorators()

例如像這樣:

class ExampleForm extends Core_Form 
    { 
     public function init() 
     { 
      //Example Field 
      $example = new Zend_Form_Element_Hidden('example'); 
      $this->addElement($example); 
     } 

     public function loadDefaultDecorators() 
     { 
      $this->example->setDecorators(array('ViewHelper')); 
     } 
    } 
+0

謝謝!我不知道爲什麼我沒有考慮重寫loadDefaultDecorators()函數。 – leek 2008-12-18 13:17:52

3

將表單元素的裝飾器重置爲僅使用'ViewHelper'。例如:

<?php echo $this->exampleForm->example->setDecorators(array('ViewHelper')) ; ?> 

顯然,視圖並不是理想的地方,但您明白了。請注意,調用setDecorator *** s ***()會重置所有裝飾器,而不是添加新的裝飾器。

3

如果禁用隱藏元素的DD/dt的裝飾,你就會有無效XHTML,因爲你會在dl中看到某些不是有效的項目。唯一的解決方案是在所有表單元素上禁用這些裝飾器,而不僅僅是隱藏的,並在整個表單上禁用它們。爲了保持一致性,您需要在所有表格中執行此操作。

恕我直言,這是ZF的一個糟糕的設計決定。我的意思是說,輸入的價值是一個「術語」的「定義」在語義上是一個可愛的想法,但它沒有完全思考。

同樣的問題在這裏:Zend Framework: How do I remove the decorators on a Zend Form Hidden Element?

0

這是我做的:

class M_Form_Element_Hidden extends Zend_Form_Element_Hidden { 
    public function init() { 
     $this->setDisableLoadDefaultDecorators(true); 
     $this->addDecorator('ViewHelper'); 
     $this->removeDecorator('DtDdWrapper'); 
     $this->removeDecorator('HtmlTag'); 
     $this->removeDecorator('Label'); 
     return parent::init(); 
    } 
} 

然後在你的表格,

$element = new M_Form_Element_Hidden('myElement'); 
$this->addElement($element); 

Source

1

如果您要添加元素這樣:

$this->addElement(
    'text', 
    'a1', 
    array('required' => true, 'validators' => array('Alpha')) 
); 

你可以走出dd/dt標籤與此的每一個元素:

$this->setElementDecorators(array('ViewHelper')); 

,或者如果你是goint在這個其他方式補充元素:

$nombre1 = new Zend_Form_Element_Text(
      'n1', 
      array('id'=> 'Nombre1', 'validators' => array('Alpha')) 
      ); 
//$nombre1->setDecorators(array('ViewHelper')); 
$this->addElement($nombre1); 

您必須解除:

//$nombre1->setDecorators(array('ViewHelper')); 

爲了禁用dd/dt個標籤。 這最後一種方法只是禁用當前元素,表單中的其他元素保持標記正常。