Zend 框架:在我的表单中插入 DIV 和图像 [英] Zend Framework: Insert DIV and Image in my form

查看:29
本文介绍了Zend 框架:在我的表单中插入 DIV 和图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的 zend 表单中插入这样的 html 代码:

<div class="myClass1" id="myId1" style="display: none;"><img src="images/myImage.jpg"/></div>

上述html代码在zend框架中的代码是什么.我尝试使用 Create_Element 但它总是在 div 标签内创建一个按钮.请给出示例代码.谢谢

解决方案

我创建了一个名为html"的自定义元素.我将 Html.php 添加到/Form/Element:

<前><代码>/** Zend_Form_Element_Xhtml */require_once 'Zend/Form/Element/Xhtml.php';/*** HTML 表单元素**/Zend_Form_Element_Html 类扩展 Zend_Form_Element_Xhtml{/*** 用于渲染的默认表单视图助手* @var 字符串*/公共 $helper = 'formHtml';}

其次我必须添加一个视图助手 FormHtml.php(我把它放在 application/views/helpers 中):

<前><代码>/*** 扩展抽象类*/require_once 'Zend/View/Helper/FormElement.php';/*** 显示 HTML 的助手**/Zend_View_Helper_FormHtml 类扩展 Zend_View_Helper_FormElement{/*** 帮助在表单中显示 html** @param string|array $name 如果是字符串,则为元素名称.如果* 数组,其他所有参数都被忽略,数组元素* 被提取以代替添加的参数.** @param混合 $value 元素值.** @param array $attribs 元素标签的属性.** @return string 元素 XHTML.*/公共函数 formHtml($name, $value = null, $attribs = null){$info = $this->_getInfo($name, $value, $attribs);提取($信息);//名称、值、属性、选项、列表、禁用//渲染按钮.$xhtml = 'view->escape($id) .'">'.$this->_htmlAttribs($attribs).$this->view->escape($value) .'';返回 $xhtml;}}

然后您可以将 html 添加到表单中,如下所示:

<前><代码>$form->createElement('html', 'someid', array('value'=>'gna

foobar

'));

可能会有更多的简化.

I need to insert html code like this in my zend form:

<div class="myClass1" id="myId1" style="display: none;"><img src="images/myImage.jpg" /></div>

What will be the code in zend framework for above html code. I tried with Create_Element but it always create a button inside a div tag. Please give an example code. Thanks

解决方案

I created a custom element called "html". I added Html.php to /Form/Element:


/** Zend_Form_Element_Xhtml */
require_once 'Zend/Form/Element/Xhtml.php';

/**
 * HTML form element
 * 
 */
class Zend_Form_Element_Html extends Zend_Form_Element_Xhtml
{
    /**
     * Default form view helper to use for rendering
     * @var string
     */
    public $helper = 'formHtml';
}

Second I had to add a view helper FormHtml.php (I put it in application/views/helpers):


/**
 * Abstract class for extension
 */
require_once 'Zend/View/Helper/FormElement.php';

/**
 * Helper to show HTML
 *
 */
class Zend_View_Helper_FormHtml extends Zend_View_Helper_FormElement
{
    /**
     * Helper to show a html in a form
     *
     * @param string|array $name If a string, the element name.  If an
     * array, all other parameters are ignored, and the array elements
     * are extracted in place of added parameters.
     *
     * @param mixed $value The element value.
     *
     * @param array $attribs Attributes for the element tag.
     *
     * @return string The element XHTML.
     */
    public function formHtml($name, $value = null, $attribs = null)
    {
        $info = $this->_getInfo($name, $value, $attribs);
        extract($info); // name, value, attribs, options, listsep, disable

        // Render the button.
        $xhtml = 'view->escape($id) . '">'
            . $this->_htmlAttribs($attribs)
            . $this->view->escape($value) . '';

        return $xhtml;
    }
}

You can then add html to your form as follows:


$form->createElement('html', 'someid', array('value'=>'gna

foobar

'));

There might be some more simplifications possible.

这篇关于Zend 框架:在我的表单中插入 DIV 和图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆