我想在Zend表单中添加一个纯文本节点,目的是仅显示一些静态文本。
问题在于 - 我不知道有任何可以这样做的方法。
我已经使用了“description”,但那必须附加到一个表单元素上。
有没有简单地将一些文本作为表单的一部分显示的方法?Zend将所有内容视为表单元素,因此我不能只打印出来。
例如:
以下将测试您对某事物的能力。 . . .
等等...
有什么想法吗?
我想在Zend表单中添加一个纯文本节点,目的是仅显示一些静态文本。
问题在于 - 我不知道有任何可以这样做的方法。
我已经使用了“description”,但那必须附加到一个表单元素上。
有没有简单地将一些文本作为表单的一部分显示的方法?Zend将所有内容视为表单元素,因此我不能只打印出来。
例如:
以下将测试您对某事物的能力。 . . .
等等...
有什么想法吗?
Zend提供了一个表单注释视图助手(Zend_View_Helper_FormNote),您可以使用它来添加文本。
只需创建一个新的表单元素(/application/forms/Element/Note.php):
class Application_Form_Element_Note extends Zend_Form_Element_Xhtml
{
public $helper = 'formNote';
}
在你的表单中:
$note = new Application_Form_Element_Note(
'test',
array('value' => 'This is a <b>test</b>')
);
$this->addElement($note);
`public function isValid($value){
return true;
}`
这样,在验证过程中,该元素就不会消失了。 - juque添加一个带有非转义描述的隐藏元素即可。
$form->addElement('hidden', 'plaintext', array(
'description' => 'Hello world! <a href="#">Check it out</a>',
'ignore' => true,
'decorators' => array(
array('Description', array('escape'=>false, 'tag'=>'')),
),
));
完美运作。它仍然附加在一个元素上,但是不会以这种方式呈现。
代码来自:http://paveldubinin.com/2011/04/7-quick-tips-on-zend-form/
可能有更好的方法,但我通过使用自定义表单元素和视图助手创建了一个段落。对于这么简单的东西来说,似乎需要很多代码。如果您找到了更简单的方法,请告诉我。
//From your form, add the MyParagraph element
$this->addElement(new Zend_Form_Element_MyParagraph('myParagraph'));
class Zend_Form_Element_MyParagraph extends Zend_Form_Element
{
public $helper = 'myParagraph';
public function init()
{
$view = $this->getView();
}
}
class Zend_View_Helper_MyParagraph extends Zend_View_Helper_FormElement {
public function init() {
}
public function myParagraph() {
$html = '<p>hello world</p>';
return $html;
}
}
有点晚了,但我仍然希望为社区做出贡献。
Aine说得一点不错。如果您想在Zend_Form中使用文本,则需要使用FormNote。但是,您可以在不需要扩展Zend_Form_Element_Xhtml的情况下使用它。请参见以下示例:
$text = new Zend_Form_Element_Text('myformnote');
$text->setValue("Text goes here")
->helper = 'formNote';
$note = new Zend_Form_Element_Note('forgot_password');
$note->setValue('<a href="' . $this->getView()->serverUrl($this->getView()->url(array('action' => 'forgot-password'))) . '">Forgot Password?</a>');
$helper
与相同的值。 - danronmoon我遇到了同样的问题,并决定不再使用Zend_Form,而是直接使用视图帮助程序(就像Ruby on Rails一样),并在模型上进行验证。
$objectForm->addElement(new Zend_Form_Element_Note('note', array('value' => 'Hello World')));