我正在切换表单以使用 View 脚本作为装饰器。 到目前为止我看到的示例在 View 脚本中执行以下操作:
<td><label for='textEmail'>Email:</label></td>
<td><?php echo $this->element->textEmail; ?></td>
我想找到一种方法,让表单对象中的文本也显示在标签中。
class RegisterForm extends Zend_Form {
public function init () {
$this->setAction('')
->setMethod('post')
->setAttrib('id','formRegister');
$this->addElement('text', 'textEmail', array('label' => 'Email: '));
$oEmail = $this->getElement('textEmail')
->setRequired(true)
->addFilter('StringTrim')
->addValidator('EmailAddress');
$oEmail->setDecorators(array('ViewHelper', 'Errors'));
$this->setDecorators(array(array('ViewScript', array('viewScript' => 'forms/RegisterForm.phtml'))));
}
}
以上是我的表单对象的定义方式。 有谁知道如何访问定义的标签值? 也许采用以下格式?
<?php echo $this->element->textEmail->label; ?>
这当然行不通。 :p 谢谢~
最佳答案
$this->元素->getLabel()
这是我的标准字段 View 脚本:
<div class="field<?php if($this->element->hasErrors()): ?> errors<?php endif; ?>" id="field_<?php echo $this->element->getId(); ?>">
<?php if (0 < strlen($this->element->getLabel())) : ?>
<?php echo $this->formLabel($this->element->getName(), $this->element->getLabel());?>
<?php endif; ?>
<span class="value"><?php echo $this->{$this->element->helper}(
$this->element->getName(),
$this->element->getValue(),
$this->element->getAttribs()
) ?></span>
<?php if ($this->element->hasErrors()) : ?>
<?php echo $this->formErrors($this->element->getMessages()); ?>
<?php endif; ?>
<?php if (0 < strlen($this->element->getDescription())) : ?>
<span class="hint"><?php echo $this->element->getDescription(); ?></span>
<?php endif; ?>
</div>
关于scripting - Zend Form - 从表单类中提取标签值以在 View 脚本中显示的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1398102/