我正在使用扩展Zend_Form的类构建一个表单。如何在表单中添加一个img标签?我还需要为它添加一个类并对齐属性
这是我想要达到的最终结果:
<span class="myElement"><img src="myPath" align="middle" class="myClass"/>
<input type="text"></span>
我没有找到关于Zend_Form_Element_Image文档的详细信息
感谢
卢卡
答案 0 :(得分:10)
有库/应用程序/表单/元素/ Img.php
class Application_Form_Element_Img extends Zend_Form_Element_Xhtml
{
public $helper = 'formImg';
public function loadDefaultDecorators ()
{
parent::loadDefaultDecorators ();
$this->removeDecorator ('Label');
$this->removeDecorator ('HtmlTag');
$this->addDecorator('HtmlTag', array (
'tag' => 'span',
'class' => 'myElement',
));
}
}
在application / view / helpers / FormImg.php
中class Zend_View_Helper_FormImg extends Zend_View_Helper_FormElement
{
public function formImg ($name, $value, $attribs = null)
{
$info = $this->_getInfo($name, $value, $attribs);
$xHtml = '<img'
. $this->_htmlAttribs ($attribs)
. ' />';
return $xHtml;
}
}
以您的形式:
$this->addElement ('img', 'myimage', array (
'src' => '/images/download.png',
'align' => 'right',
));
注意:路径可能会在您的特定应用中发生变化。
答案 1 :(得分:3)
实际上,您不需要自定义元素来执行此操作。您可以使用HtmlTag装饰器并使用openOnly选项。
$form = new Zend_Form();
$form->addElement("text", "foo", array("decorators" => array(
array(array("img" => "HtmlTag"), array(
"tag" => "img",
"openOnly" => true,
"src" => "myPath",
"align" => "middle",
"class" => "myClass"
)),
array("ViewHelper"),
array(array("span" => "HtmlTag"), array(
"tag" => "span",
"class" => "myElement"
))
)));
echo $form->foo;
答案 2 :(得分:2)
您好,您可以创建一个名为“html”的自定义元素
class Zend_Form_Element_Html extends Zend_Form_Element_Xhtml
{
public $helper = 'formHtml';
}
现在你可以打电话给它:
$yourForm->addElement(
'html',
'myElementId',
array(
'value'=>'<span class="myElement"><img src="myPath" align="middle" class="myClass"/>
<input type="text"></span>'))
有关详细信息,请查看此链接:
Zend Framework: Insert DIV and Image in my form