如何在zend表单中添加"纯文本节点"?

Mal*_*yer 20 zend-form zend-form-element

我正在尝试以zend形式添加纯文本节点 - 目的是仅显示一些静态文本.

问题是 - 我不知道有任何这样的方法来做到这一点.

我使用了'description'但是要将它附加到表单元素.

有没有办法简单地将一些文本显示为表单的一部分?Zend将所有内容都视为表单元素,因此我无法将其打印出来.

例如:

以下将测试你的能力....

等等...

有什么想法吗?

Ain*_*ine 38

Zend有一个表单注释视图助手(Zend_View_Helper_FormNote),您可以使用它来添加文本.

只需创建一个新的表单元素(/application/forms/Element/Note.php):

class Application_Form_Element_Note extends Zend_Form_Element_Xhtml  
{  
    public $helper = 'formNote';  
}
Run Code Online (Sandbox Code Playgroud)

在你的形式:

$note = new Application_Form_Element_Note(
    'test',
    array('value' => 'This is a <b>test</b>')
);
$this->addElement($note);
Run Code Online (Sandbox Code Playgroud)

  • 在类中添加此函数:`public function isValid($ value){return true; 这样,元素不会在验证过程中消失. (11认同)

小智 9

添加具有非转义描述的隐藏元素就可以了.

$form->addElement('hidden', 'plaintext', array(
    'description' => 'Hello world! <a href="#">Check it out</a>',
    'ignore' => true,
    'decorators' => array(
        array('Description', array('escape'=>false, 'tag'=>'')),
    ),
));
Run Code Online (Sandbox Code Playgroud)

完美的工作.它仍然附加到一个元素,然而,这个元素不会以这种方式呈现.

代码取自:http://paveldubinin.com/2011/04/7-quick-tips-on-zend-form/

  • 这就是我喜欢Zend的原因.不,不是真的. (8认同)

Chr*_*ris 6

可能有更好的方法,但我使用自定义表单元素和视图助手创建了一个段落.似乎有很多简单的代码.如果你找到了一种更简单的方法,请告诉我.

//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;
    }

}
Run Code Online (Sandbox Code Playgroud)


Rij*_*ael 6

有点晚了,但我想我会为了社区的利益而扔掉它.

艾恩已经击中了头部.如果要在Zend_Form中使用文本,则需要FormNote.但是,您可以使用它而无需扩展Zend_Form_Element_Xhtml.见下面的例子:

$text = new Zend_Form_Element_Text('myformnote');
$text->setValue("Text goes here")
     ->helper = 'formNote';
Run Code Online (Sandbox Code Playgroud)

请注意,您可以将text和html与formNote帮助器一起使用.


axi*_*m82 5

此功能通过Zend_Form_Element_Note内置到Zend中.

$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>');
Run Code Online (Sandbox Code Playgroud)