Zend framework 在Zend Framework中向文本字段添加美元符号
有没有办法在Zend_表单元素的Zend framework 在Zend Framework中向文本字段添加美元符号,zend-framework,zend-form,Zend Framework,Zend Form,有没有办法在Zend_表单元素的前面添加一个“$”呢?(当然,使用标准的ZF材料会很棒) 编辑:例如,如果Zend_表单为成本元素生成的html类似:(非常简化) 成本: 我希望它输出以下内容: <label>Cost:</label> $ <input type="text" name="cost" /> 成本: $ 您可以将其添加为说明 $this->createElement('text', 'cost') ->setLabe
前面添加一个“$”呢?(当然,使用标准的ZF材料会很棒)
编辑:例如,如果Zend_表单为成本
元素生成的html类似:(非常简化)
成本:
我希望它输出以下内容:
<label>Cost:</label>
$ <input type="text" name="cost" />
成本:
$
您可以将其添加为说明
$this->createElement('text', 'cost')
->setLabel('Cost:')
->setDescription('$');
然后正确配置元素的装饰器
Upd:
建议元素的装饰器:
array(
'ViewHelper',
array('Description', array('placement' => Zend_Form_Decorator_Abstract::PREPEND, 'tag' => 'em'))
);
请注意放置选项。您可以将其添加为说明
$this->createElement('text', 'cost')
->setLabel('Cost:')
->setDescription('$');
然后正确配置元素的装饰器
Upd:
建议元素的装饰器:
array(
'ViewHelper',
array('Description', array('placement' => Zend_Form_Decorator_Abstract::PREPEND, 'tag' => 'em'))
);
请注意放置选项。使用装饰器,您可以执行以下操作:
$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.
));
和往常一样,确保在表单中注册装饰器的名称空间。因此,如果您使用位于include路径上的文件My/Decorator/AnyMarkup.php
中的链接片段My\u Decorator\u AnyMarkup
中命名的类,那么您需要类似以下内容:
$form->addlementprefixpath('My_Decorator'、'My/Decorator'、'Decorator')代码>使用装饰器,您可以执行以下操作:
$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.
));
和往常一样,确保在表单中注册装饰器的名称空间。因此,如果您使用位于include路径上的文件My/Decorator/AnyMarkup.php
中的链接片段My\u Decorator\u AnyMarkup
中命名的类,那么您需要类似以下内容:
$form->addlementprefixpath('My_Decorator'、'My/Decorator'、'Decorator')代码>您可以使用decorator在元素中放入所需的任何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="" />
成本:
$
希望这能帮上忙,或者至少能为您指明正确的方向。您可以使用decorator在元素中添加任何想要的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="" />
成本:
$
希望这将是有益的,或者至少为您指明了正确的方向。请举例说明您的意思。请举例说明您的意思。这不是在
元素后添加了$符号吗?谢谢您的答复,但我无法使它起作用。。但这可能是我在表单中的其他地方做的事情(我对Zend_表单有点陌生,需要阅读更多关于decorators的内容。)这不是在
元素后添加了$符号吗?谢谢你的回复,我无法让它工作。。但这可能是我在表单中的其他地方正在做的事情(我对Zend_表单有点陌生,需要阅读更多关于decorators的内容…)回答得好,但如果它不使用任何额外的东西,我会更喜欢它。这不能用标准的描述装饰器来完成吗?答案很好,但是如果它不使用任何额外的东西,我会更喜欢它。这不能用标准的描述装饰器来完成吗?