我搜索每个地方但找不到答案,所以问题是,在哪里必须是Decorator类,让我们看一下例子:
我有application / forms / Guestbook.php
class Application_Form_Guestbook extends Zend_Form
{
public function init()
{
$this->addPrefixPath('My_Decorator', '/application/forms/decorator', 'decorator');
$this->setMethod('post');
$this->addElement('text', 'email', array(
'label' => 'Your email address',
'require' => true,
'filters' => array('StringTrim'),
'validators' => array('EmailAddress'),
//'prefixPath' => array('decorator' => array('My_Decorator' => 'application/forms/decorator')),
'decorators' => array(array('SimpleInput'))));
// Add the comment element
$this->addElement('textarea', 'comment', array(
'label' => 'Please Comment:',
'required' => true,
'validators' => array(
array('validator' => 'StringLength', 'options' => array(0, 20))
)
));
// Add a captcha
$this->addElement('captcha', 'captcha', array(
'label' => 'Please enter the 5 letters displayed below:',
'required' => true,
'captcha' => array(
'captcha' => 'Figlet',
'wordLen' => 5,
'timeout' => 300
)
));
$this->addElement('submit', 'submit', array('ignore' => true, 'label' => 'Sign Guestbook'));
$this->addElement('hash', 'csrf', array('ignore' => true));
}
}
并拥有application / forms / decorator / SimpleInput.php
class My_Decorator_SimpleInput extends Zend_Form_Decorator_Abstract
{
protected $_format = '<label for="%s">%s !!!!!!</label><input id="%s" name="%s" type="text" value="%s"/>';
public function render($content)
{
$element = $content->getElement();
$name = htmlentities($element->getFullyQualifiedName());
$label = htmlentities($element->getLabel());
$id = htmlentities($element->getId());
$value = htmlentities($element->getValue());
$markup = sprintf($this->_format, $name, $label, $id, $name, $value);
return $markup;
}
}
当我尝试启动页面时,加载装饰器时出错:
An error occurred
Application error
Exception information:
Message: Plugin by name 'SimpleInput' was not found in the registry; used paths: My_Decorator_: /application/forms/decorator/ Zend_Form_Decorator_: Zend/Form/Decorator/
Stack trace:
#0 /home/dev/public_html/local.zend.test/library/Zend/Form/Element.php(1827): Zend_Loader_PluginLoader->load('SimpleInput')
#1 /home/dev/public_html/local.zend.test/library/Zend/Form/Element.php(2207): Zend_Form_Element->_getDecorator('SimpleInput', NULL)
#2 /home/dev/public_html/local.zend.test/library/Zend/Form/Element.php(1980): Zend_Form_Element->_loadDecorator(Array, 'SimpleInput')
#3 /home/dev/public_html/local.zend.test/library/Zend/Form/Element.php(316): Zend_Form_Element->getDecorators()
#4 /home/dev/public_html/local.zend.test/library/Zend/Form/Element.php(271): Zend_Form_Element->loadDefaultDecorators()
#5 /home/dev/public_html/local.zend.test/library/Zend/Form.php(1125): Zend_Form_Element->__construct('email', Array)
#6 /home/dev/public_html/local.zend.test/library/Zend/Form.php(1035): Zend_Form->createElement('text', 'email', Array)
#7 /home/dev/public_html/local.zend.test/application/forms/Guestbook.php(18): Zend_Form->addElement('text', 'email', Array)
#8 /home/dev/public_html/local.zend.test/library/Zend/Form.php(240): Application_Form_Guestbook->init()
#9 /home/dev/public_html/local.zend.test/application/controllers/GuestbookController.php(28): Zend_Form->__construct()
#10 /home/dev/public_html/local.zend.test/library/Zend/Controller/Action.php(516): GuestbookController->signAction()
#11 /home/dev/public_html/local.zend.test/library/Zend/Controller/Dispatcher/Standard.php(308): Zend_Controller_Action->dispatch('signAction')
#12 /home/dev/public_html/local.zend.test/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#13 /home/dev/public_html/local.zend.test/library/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch()
#14 /home/dev/public_html/local.zend.test/library/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#15 /home/dev/public_html/local.zend.test/public/index.php(26): Zend_Application->run()
#16 {main}
此示例来自快速指南。问题是这些装饰器的位置(默认位置),或者我如何加载它们以及我应该写的'path / to / decorator'(来自root \ application \ forms文件夹?)因为Zend中的文档通常很难看。 .. = \
答案 0 :(得分:0)
编辑#3。
我认为问题可能是Zend不喜欢你如何设置路径。 / application是一个绝对的路径,可能无法解决您的想法。
尝试:
$this->addPrefixPath('My_Decorator', APPLICATION_PATH . '/forms/decorator', 'decorator');
那条路必须是好的。 Zend很可能正在查看/ public / application / etc ......你使用它的方式。
希望这有帮助!
答案 1 :(得分:0)
装饰器使用Autoloader加载,就像其他类一样,因此装饰器需要根据类名放置:
例如。 My_Decorator_Something
library/My/Decorator/Something.php
在这种情况下看起来最好的选择是改进你的装饰者类命名。
但是,如果您需要其他自定义位置,则需要编写并注册自己的自动加载器。