Issues

ZF-2879: Create an init() method for processing form models

Description

Currently the documented best practice for creating a Zend_Form 'model' (i.e. adding fields/validators/etc. from within the user-defined class) is to override the '__construct' method.

The documentation fails to add the critical line 'parent::__construct($options)' - a sticking point that saw about an hours' worth of trying to debug the problem (that was my own fault, though :)

Instead of requiring '__construct' would it be possible to add an 'init()' method? The benefits are:-

  • Doesn't require overriding constructor (unnecessary in a lot of instances)
  • Becomes a recommended but not required coding practice within the framework - as per Zend_Db, Zend_Controller, etc...

Suggested patch:


Index: library/Zend/Form.php
===================================================================
--- library/Zend/Form.php   (revision 8819)
+++ library/Zend/Form.php   (working copy)
@@ -207,9 +207,22 @@
         }
 
         $this->loadDefaultDecorators();
+
+        $this->init();
     }
 
     /**
+     * Initialize object
+     *
+     * Called from {@link __construct()} as final step of object instantiation.
+     *
+     * @return void
+     */
+    public function init()
+    {
+    }
+
+    /**
      * Set form state from options array
      * 
      * @param  array $options 

Comments

Scheduling for 1.5.1.

One note: I think that init() should be called not as the last item in __construct(), but the last item before the call to loadDefaultDecorators(). This would allow you to prevent the default decorators from loading if you were to create them in your init() method.

Added in trunk and 1.5 release branch; docs updated in trunk.

I'm assuming this fix is merged to the 1.5 release branch for release with 1.5.1. Please update JIRA if this is not the case.