(on Friday, 17 October 2008, 07:52 AM -0700):
> > <...snip...>
> >
> > Also, it's recommended practice to override the (empty) init method in
> > Zend_Form rather than the constructor:
> >
> > class SoftwareForm extends Zend_Form
> > {
> > public function init()
> > {
> > // Form initialisation as normal
> > }
> > }
> >
>
> couple of questions on this?
> 1) Where do I find out such recommended practices?
It's in the Zend_Form documentation, and several examples show it.
> 2) My own code breaks when I switch it to init() rather than the
> constructor... changing this
>
> public function __construct($options = null)
> {
> // Path setting for custom decorations
> $this->addElementPrefixPath('MyApp_Form_Decorator',
> 'MyApp/Form/Decorator/',
> 'decorator');
>
> // Path string for custom form elements
> $this->addPrefixPath('MyApp_Form_Element',
> 'MyApp/Form/Element/',
> 'element');
>
> $this->_setupTranslation();
>
> parent::__construct($options);
>
> $this->setAttrib('accept-charset', 'UTF-8');
> $this->setDecorators(array(
> 'FormElements',
> 'Form'
> ));
> }
>
> to this:
>
> public function init() {
> {
> // Path for custom decorations
> $this->addElementPrefixPath('MyApp_Form_Decorator',
> 'MyApp/Form/Decorator/',
> 'decorator');
>
> // Path string for custom form elements
> $this->addPrefixPath('MyApp_Form_Element',
> 'MyApp/Form/Element/',
> 'element');
>
> $this->_setupTranslation();
>
> $this->setAttrib('accept-charset', 'UTF-8');
> $this->setDecorators(array(
> 'FormElements',
> 'Form'
> ));
> }
>
> gives me errors because my paths aren't set early enough I think ('Plugin by
> name MyPluginName was not found in the registry')
>
> Am I missing something - it has me stumped as to why its not working. The
> stack shows init() being called immediately after the parent constructor
>From what I see above, you're passing in configuration that sets up your
elements -- which would explain why the path's are not setup (init() is
called at the end of __construct()). When using init(), we suggest that
you setup the entire form that way, instead of with configuration (use
configuration only to customize the form -- such as the action, method,
and name, or to specify additional plugin paths). That way, you can
have a class per form -- which makes your form re-usable.
--
Matthew Weier O'Phinney
Software Architect | matthew@zend.com
Zend Framework | http://framework.zend.com/
没有评论:
发表评论