2011-03-16 37 views
1

有沒有在<input>之前爲Zend_Form元素添加「$」的方法? (當然,使用標準ZF的東西會很棒)。在Zend Framework的文本字段中添加美元符號

EDIT:例如,如果通過Zend_Form的用於cost元件產生的HTML是這樣的:(非常簡化的)

<label>Cost:</label> 
<input type="text" name="cost" /> 

我喜歡它,以輸出此:

<label>Cost:</label> 
$ <input type="text" name="cost" /> 
+0

請舉個例子說明你的意思 – ahmedsafan86 2011-03-16 16:49:47

+0

@Ahmed增加了一個例子 – cambraca 2011-03-16 16:55:20

回答

3

您可以使用callback裝飾把任何HTML你想在你的元素:

例如,在你的情況我可以這樣做:

$el1 = $this->createElement('text', 'el1')->setLabel('Cost:'); 

    // Anonymous function that will generate your custom html (needs PHP 5.3). 
    // For older PHP there are other ways of making anonymous functions. 
    $myHtml = function($content, $element, array $options) { 
       return '$'; 
      }; 
    $el1->setDecorators(array(
     'ViewHelper', 
     'Errors', 
     array('Callback', array('callback' => $myHtml, 'placement' => 'PREPEND')), 
     'Label' 
    )); 

這將導致下面的html代碼:

<label for="el1" class="optional">Cost:</label> 
$ 
<input type="text" name="el1" id="el1" value="" /> 

希望這將是helfull或至少指出你在正確的方向。

1

您可以添加它作爲描述

$this->createElement('text', 'cost') 
    ->setLabel('Cost:') 
    ->setDescription('$'); 

然後正確配置元素的裝飾器。

UPD:

建議元素的裝飾:

array(
    'ViewHelper', 
    array('Description', array('placement' => Zend_Form_Decorator_Abstract::PREPEND, 'tag' => 'em')) 
); 

注意放置選項。

+0

這不是加了$ sign _after_after_''元素嗎? – cambraca 2011-03-16 17:13:54

+0

@cambraca:請參閱更新的答案 – Vika 2011-03-16 17:37:57

+0

感謝您的回覆,雖然我無法讓它工作..但它可能是我在表單其他地方做的事情(我對Zend_Form有點新鮮,需要閱讀一些更多關於裝飾者..) – cambraca 2011-03-16 22:13:47

3

使用AnyMarkup裝飾,你可以做一些這樣的:

$element->setDecorators(array(
    'ViewHelper', 
    array('AnyMarkup', array('markup' => '$', 'placement' => 'prepend')), 
    // And any other decorators, like Label, Description, Errors, and 
    // other wrapping like td, tr, etc. 
)); 

像往常一樣,一定要註冊的命名空間與形式的裝飾。所以,如果你使用的是位於該文件My/Decorator/AnyMarkup.php在包括路徑上的鏈接片段My_Decorator_AnyMarkup命名類,然後你需要這樣的:

$form->addElementPrefixPath('My_Decorator_', 'My/Decorator', 'decorator');

+0

好的答案,但如果它沒有使用任何額外的東西,我會更喜歡它。這不能用標準的描述裝飾器來完成嗎? – cambraca 2011-03-16 17:29:39