我正在尝试在 Zend Forms 中创建类型为 email
的输入,但我无法完成预期的结果。
我创建了一个自定义类:
namespace AAA\Forms\Elements;
class Email extends \Zend_Form_Element_Text
{
function __construct($name, $label, $required)
{
parent::__construct($name);
$this->setAttrib('type', 'email');
$this->setLabel($label)->setRequired($required);
}
}
然后我像这样使用它:
class Application_Form_Register extends Zend_Form
{
public function init()
{
// …
$email = new AAA\Forms\Elements\Email('email', 'E-mail:', true);
$this->addElement($email);
// …
}
}
我得到的是:
<input type="text" name="email" id="email" value="" type="email">
注意两个 type
参数。
我已经在 Bootstrap.php 中设置了 HTML5 文档类型($documentType = new Zend_View_Helper_Doctype(); $documentType->doctype('HTML5');
)并且我有也试过这个:
function __construct($name, $label, $required)
{
$options = array();
$options['type'] = 'email';
parent::__construct($name, $options);
// …
}
我读过 Zend HTML5 Form element types但没有一个答案有效。我也尝试过 Glitch 库,但结果是一样的。
有人知道如何强制 Zend Framework 使用 HTML5 表单控件吗?
最佳答案
您还需要实现自己的 View 助手来呈现电子邮件表单元素。
Zend_Form_Element_Text
使用 formText
查看助手 ( Zend/View/Helper/FormText.php
) 来呈现 HTML 输入,它被硬编码为输出 <input type="text"
渲染元素时。
两种可能的处理方式是:
- 删除
ViewHelper
来自元素的装饰器并使用ViewScript
渲染元素的助手。 - 制作自己的 View 助手
formEmail
几乎与formText
相同除了它输出type="email"
;并设置公众$helper
Element 类中的属性为formEmail
.您将必须注册到formEmail
的路径助手使用Zend_View::addHelperPath()
所以它将被ViewHelper
找到装饰师。
关于php - Zend Forms 电子邮件元素更改为文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11382954/