2011-03-16 52 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