我目前使用zend_decorators向我的表單添加樣式。我想知道是否有其他方法可以做到這一點?編寫裝飾器有點困難。我喜歡使用的div和css樣式休閒的一個:有沒有更好的方式來設計zend_forms而不是使用裝飾器?
<input type="submit" class="colorfulButton" >
這是很簡單的,而不是設置一定的控制裝飾和添加。因爲它需要爲每個樣式實現創建一個裝飾器並將其與控件相加。將看助手的伎倆?
我目前使用zend_decorators向我的表單添加樣式。我想知道是否有其他方法可以做到這一點?編寫裝飾器有點困難。我喜歡使用的div和css樣式休閒的一個:有沒有更好的方式來設計zend_forms而不是使用裝飾器?
<input type="submit" class="colorfulButton" >
這是很簡單的,而不是設置一定的控制裝飾和添加。因爲它需要爲每個樣式實現創建一個裝飾器並將其與控件相加。將看助手的伎倆?
有幾種方法。你可以推出你自己的元素視圖助手(我猜可能會很笨拙)。
或者......你可以使用一個viewscript的形式,像這樣的(非常簡單的例子):
class Your_Form extends Zend_Form
{
public function init()
{
$this->setDecorators(array(
'PrepareElements',
array('ViewScript', array('viewScript' => 'path/to/viewscript.phtml'))
));
// only use basic decorators for elements
$decorators = array(
'ViewHelper',
'Label',
'Errors'
);
// create some element
$someElement = new Zend_Form_Element_Text('someElement');
// set the basic decorators for this element and set a css class
$someElement->setDecorators($decorators)
->setAttrib('class', 'someCssClass');
// add (potentially multiple) elements to this from
$this->addElements(array(
$someElement
));
}
}
見standard decorators section about PrepareElements爲什麼它需要有對的形式,當PrepareElements裝飾集使用ViewScript裝飾器。
然後在viewscript:
<?
// the form is available to the viewscript as $this->element
$form = $this->element;
?>
<!-- put whatever html you like in this script and render the basic element decorators seperately -->
<div>
<? if($form->someElement->hasErrors()): ?>
<?= $form->someElement->renderErrors() ?>
<? endif; ?>
<?= $form->someElement->renderLabel(); ?>
<?= $form->someElement->renderViewHelper(); ?>
</div>
如果你只是想在表單元素上設置一個類屬性,就沒有必要定義一個裝飾器了:這可以使用zend_form元素的一些標準方法來完成。
見setAttrib()
方法,在手冊的部分Metadata and Attributes,並給了有的例子(引用):
// Equivalent to $element->setAttrib('class', 'text'):
$element->class = 'text;
如果你可以設置一個類屬性這樣,您可以在構建表單元素時或在定義這些元素的.ini文件中對其進行設置 - 有一個示例在頁面稍後的Configuration部分中顯示。
我同意這個答案簡單的情況。但是如果我們有某種設計複雜性呢?就像分層div與每層不同的表單元素和類似。 – Hanseh 2010-04-12 09:26:49
在這個(複雜)情況下,我想你必須使用裝飾器,是的。 – 2010-04-12 10:42:27
謝謝,這是我需要的。乾杯 – Hanseh 2010-04-14 03:51:33