【发布时间】:2011-06-22 00:27:18
【问题描述】:
是否有任何简单的方法可以将 HTML5 表单元素转换为 Zend 表单? createElement('tel','phone');只是不起作用,因为 zend 不支持 html5 表单元素,但它似乎......你不能在创建后覆盖 type 属性。
例如我需要在我的 zend 表单上输入 type="tel/email/date/number" 等。
【问题讨论】:
是否有任何简单的方法可以将 HTML5 表单元素转换为 Zend 表单? createElement('tel','phone');只是不起作用,因为 zend 不支持 html5 表单元素,但它似乎......你不能在创建后覆盖 type 属性。
例如我需要在我的 zend 表单上输入 type="tel/email/date/number" 等。
【问题讨论】:
没关系,我找到了这篇文章:http://www.enrise.com/2010/12/html5-zend-framework-form-elements/。我只需要将他的代码复制到我的 MVC 架构中,它现在就可以工作了。除了我必须将 $this->setAttrib('type','number') 例如放在 Number.php 中,因为它不会添加选项,除非它在控制器中设置,这有点愚蠢,因为我正在创建一个新的 Glitch_Form_Element_Text_Number 或任何 o_O。
【讨论】:
type。 $documentType = new Zend_View_Helper_Doctype(); $documentType->doctype('HTML5');
if(!isset($elem->getAttrib('type')) 或类似的东西......
本诺,
这是我为使 Glitch 工作所做的工作:(Zend Framework 1.11)
在我的控制器 init() 函数中
//someController 扩展 Zend_Controller_Action
public function init(){
$this->view->addHelperPath('Glitch/View/Helper', 'Glitch_View_Helper_');
}
然后在我的元素创建中,我只是用 attrib('type','email') 覆盖类型
public function getEmailField(){
$element = new Zend_Form_Element_Text('email');
$element->setLabel('Email');
$element->setAttrib("type", "email");
$element->setRequired();
return $element;
}
不设置视图助手,输入变为文本。
谢谢!
【讨论】:
我创建了一个writeup,展示了如何最好地使用 Enrise HTML5 表单元素,重申您需要指定 HTML5 文档类型这一事实。
【讨论】:
这是我为解决 XML5 元素所做的。
首先我在以下位置创建了一个自定义表单元素:library/Custom/Form/Element/Html5.php
<?php
/** Zend_Form_Element_Xhtml */
require_once 'Zend/Form/Element/Xhtml.php';
class Custom_Form_Element_Html5 extends Zend_Form_Element_Xhtml
{
public $helper = 'formHtml5';
}
然后我在以下位置创建了一个自定义视图助手:library/Custom/View/Helper/FormHtml5.php
<?php
/**
* Abstract class for extension
*/
require_once 'Zend/View/Helper/FormElement.php';
/**
* Helper to generate an "Html5" element
*
*/
class Custom_View_Helper_FormHtml5 extends Zend_View_Helper_FormElement
{
public function formHtml5($name, $value = null, $attribs = null)
{
$info = $this->_getInfo($name, $value, $attribs);
extract($info); // name, value, attribs, options, listsep, disable
// build the element
$disabled = '';
if ($disable) {
// disabled
$disabled = ' disabled="disabled"';
}
// XHTML or HTML end tag?
$endTag = ' />';
if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
$endTag= '>';
}
$xhtml = '<input'
. ' type="' . (($attribs['type'])?($this->view->escape($attribs['type'])):'text') . '"'
. ' name="' . $this->view->escape($name) . '"'
. ' id="' . $this->view->escape($id) . '"'
. ' value="' . $this->view->escape($value) . '"'
. $disabled
. $this->_htmlAttribs($attribs)
. $endTag;
return $xhtml;
}
}
然后在表格中我添加了这个:
class Application_Form_UserBasic extends Zend_Form
{
public function init()
{
// this will tell zf to look for custom helpers on your custom library
$view = $this->getView();
$view->addHelperPath(APPLICATION_PATH.'/../library/Custom/View/Helper/', 'Custom_View_Helper');
/* Some other code */
$email = new Custom_Form_Element_Html5('email');
$email->setAttribs(array( 'type' => 'email'));
/* Your other elements*/
$this->addElements(array(
$email, /* your other elements */
));
}
}
如果您还没有这样做,请不要忘记将此行添加到您的 application.ini 文件中:
autoloaderNamespaces[] = "Custom_"
我希望它对某人有所帮助。
【讨论】:
由于没有“开箱即用”的解决方案,我自己编写并制作了一个作曲家包以便于分发。 如果你使用composer,可以使用https://github.com/Alez/html5input
php $formElement->setAttrib('type', 'email') 将其“type”属性设置为您想要的任何内容。这就是它和 Zend_Form_Element_Text 的唯一区别,你可以把它当作简单的文本元素。示例代码:
class My_Form extends Zend_Form
{
public function init()
{
$email = new Html5Input_Element_AnyType('email', array(
'type' => 'email',
));
$this->addElement($email);
}
}
【讨论】: