我需要插入這樣的HTML代碼在我的Zend形式:Zend框架:插入DIV和圖像在我的形式
<div class="myClass1" id="myId1" style="display: none;"><img src="images/myImage.jpg" /></div>
會有什麼在上面的html代碼Zend框架的代碼。我嘗試了Create_Element,但它總是在div標籤內創建一個按鈕。請給出一個示例代碼。謝謝
我需要插入這樣的HTML代碼在我的Zend形式:Zend框架:插入DIV和圖像在我的形式
<div class="myClass1" id="myId1" style="display: none;"><img src="images/myImage.jpg" /></div>
會有什麼在上面的html代碼Zend框架的代碼。我嘗試了Create_Element,但它總是在div標籤內創建一個按鈕。請給出一個示例代碼。謝謝
我創建了一個名爲「html」的自定義元素。我加Html.php到/表格/元素:
/** 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';
}
其次,我不得不添加一個視圖助手FormHtml.php(我把它放在應用程序/視圖/傭工):
/**
* 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;
}
}
你可以然後將html添加到表單中,如下所示:
$form->createElement('html', 'someid', array('value'=>'gnafoobar
'));
可能還有一些更簡化的可能。
我通常使用數組(escape => false)的描述來在窗體中呈現HTML。
你有沒有考慮過一個表單ViewScript?見this discussion。
如果您要創建一個表單元素,我認爲你必須覆蓋isValid()方法,如下面的代碼,或者你的「價值」就會消失在驗證錯誤:
class RenomoZF_Form_Element_Note extends Zend_Form_Element_Xhtml
{
/**
* Default form view helper to use for rendering
* @var string
*/
public $helper = 'formNote';
public function isValid($value, $context = null) {
return TRUE;
}
}
非常感謝!這幫了我很多忙!無法弄清楚爲什麼我的筆記在提交時消失了。 :) – Luke 2012-05-04 04:51:29
omg你是我的英雄! – 2012-09-06 05:33:25
有同樣的問題,只是用tomas.fejfar評論以下內容。
array(escape => false)
e.g.
$decorators = array(
'ViewHelper',
'Label',
array('Description', array('escape' => false,)),
'Errors')
);
工作一種享受。
乾杯
這是準備完成在ZF 1.10。似乎有人制作了一個Zend_View_Helper_FormNote類,以便您可以插入任意(裝飾器的cept)HTML。
要使用它,您必須擴展Zend_Form_Element_Xhtml。
<?php
/** Zend_Form_Element_Xhtml */
require_once 'Zend/Form/Element/Xhtml.php';
/**
* Note form element
*
*/
class MyNS_Form_Element_Note extends Zend_Form_Element_Xhtml
{
/**
* Default form view helper to use for rendering
* @var string
*/
public $helper = 'formNote';
}
然後,使用形式插件加載,表單/元件類添加到表格對象。我把它放在我的庫文件夾(MyNs/Form/Element/Note.php)中。
$yourForm= new Zend_Dojo_Form();
$yourForm->addPrefixPath("MyNS_Form", "MyNS/Form/");
現在,您可以將它稱爲任何元素。
$yourForm->addElement(
'note',
'myElementId',
array(
'value'=>'<a href="#">omgwtfbbq</a>'
)
)
正如我之前提到的,這仍然在包裝裝飾你的代碼,但它的解決方案預建到ZF。
+1對於omgwtfbbq – 2012-03-15 14:06:32
使用上述方法,在提交帶有驗證錯誤的表單時,FormNote
的值仍然消失!
編輯:可刪除,覆蓋isValid()
問題不復存在。
請注意原始問題的日期。近兩年前,這個問題得到了公認的答案。 – 2011-05-23 16:34:53
感謝您的回答。它幫助了我。 – NAVEED 2009-11-12 19:30:43