本文共 2000 字,大约阅读时间需要 6 分钟。
Here is what I did to solve for XML5 elements.
First I created a custom form element on: library/Custom/Form/Element/Html5.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';
}
Then I created a custom view helper on: library/Custom/View/Helper/FormHtml5.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 = '
. ' 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;
}
}
Then in the Form I added this: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 */
));
}
}
Do not forget to add this line to your application.ini file in case you have not done so:
autoloaderNamespaces[] = "Custom_"
I hope it helps somebody.
转载地址:https://blog.csdn.net/weixin_30596151/article/details/117942556 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!