سؤال

I search every where but cannot find an answer, so question is, where must be Decorator class, let's look with example:

I have 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));
    }

}

and have 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;
    }

}

When I try to launch page I have an error on loading of decorator:

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}  

This example is from quick guide. Question is where must be location of such decorators (default location), or how can I load them and which 'path/to/decorator' I should write (from root\application\forms folder?) because documentation in Zend as usually ugly... =\

هل كانت مفيدة؟

المحلول

edit #3.

I think the problem might be that Zend does not like how you set the path. /application is a absolute path and would probably not resolve where you think.

Try:

$this->addPrefixPath('My_Decorator', APPLICATION_PATH . '/forms/decorator', 'decorator');

That way the path must be good. Its probable that Zend was looking in /public/application/etc... the way you have used it.

Hope this help!

نصائح أخرى

Decorators are loaded using Autoloader like the other classes, so the decorator needs to be put according to the class name:

eg. class My_Decorator_Something in library/My/Decorator/Something.php

Looks like the best option in this case is to improve your decorator class naming.

However, if you need some other, custom location, you need to write and register your own autoloader.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top