Zend表单:添加文本字段右侧的链接

5 php zend-framework zend-form

我意识到我应该能做到这一点,但我能说什么,我不明白.我甚至rtfm'ed直到我的眼睛油炸.我通过实例学习得最好,而不是Zend的文档给出的深层解释,或者这类问题通常产生的典型的"使用装饰者"反应.我需要的是这样的标记:

<dt>
    <label for="name">Name</label>
</dt>
<dd>
    <input type="text" name="name" id="name" value="">
    <a href="#">My Link</a>
</dd>
Run Code Online (Sandbox Code Playgroud)

除了输入后的额外LINK外,它全部都是香草味.是的,它在dd内部,就在链接旁边,这就是我无法实现的.

这是我用来创建上述HTML的(略微修改过的)代码

$name = new Zend_Form_Element_Text( 'name' );
$name->setLabel( 'Name' );        
$this->addElements( $name );
$this->addDisplayGroup( array( 'name' ), 'people');
Run Code Online (Sandbox Code Playgroud)

任何示例代码或更好的解释将使这个菜鸟非常高兴.

干杯!

mon*_*zee 24

请参阅我在邮件列表中的此主题的回复以及我的博客文章.这与Aaron描述的过程基本相同.

你也可以去装饰器方式,使用description属性来保存链接(未测试):

<?php
$foo = new Zend_Form_Element_Text('name');
$foo->setLabel('Name')
    ->setDescription('<a href="#">Link</a>')
    ->setDecorators(array(
        'ViewHelper',
        array('Description', array('escape' => false, 'tag' => false)),
        array('HtmlTag', array('tag' => 'dd')),
        array('Label', array('tag' => 'dt')),
        'Errors',
      ));
$form->addElement($foo);
Run Code Online (Sandbox Code Playgroud)

我不知道,如果'tag'=>falseDescription装饰会的工作,但它是值得一试.对不起,我现在无法测试,我的开发盒目前已被打破.如果失败,请尝试这两个链接中描述的手动装饰器渲染方法.


小智 5

我想你正在寻找通过View Script完全控制装饰器:

Zend框架手册

基本上,您希望将元素的viewScript属性设置为脚本的路径,然后传递您要发送的任何其他信息,可能是您正在构建的链接的linkHref或标题.

$name = new Zend_Form_Element_Text( 'name' );
$name->setLabel( 'Name' );   
$name->viewScript = 'path/to/viewScript.phtml';
$name->decorators = array('ViewScript', array('linkHref' => '#',
                                              'linkTitle' => 'My Link');
$this->addElements( $name );
$this->addDisplayGroup( array( 'name' ), 'people');
Run Code Online (Sandbox Code Playgroud)

然后你的viewScript看起来像这样,我不确定所有Helpers是如何在Zend_Form_Element的实例中,但它有点像这样:

<dt><?= $this->formLabel($this->element->getName(),
                     $this->element->getLabel()) ?></dt>
<dd><?= $this->{$this->element->helper}(
                     $this->element->getName(),
                     $this->element->getValue(),
                     $this->element->getAttribs()
                ) ?>
<a href="<?= $this->linkHref; ?>"><?= $this->linkTitle; ?></a>
<?= $this->formErrors($this->element->getMessages()) ?>

</dd>
Run Code Online (Sandbox Code Playgroud)

有时,在viewScript中更好地完成它,因为它可以让你100%控制元素,同时仍然非常干净.