Zend Framework

Create an init() method for processing form models

Details

  • Type: Improvement Improvement
  • Status: Resolved Resolved
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.5.0RC1
  • Fix Version/s: 1.5.1
  • Component/s: Zend_Form
  • Labels:
    None
  • Fix Version Priority:
    Should Have

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

Activity

Hide
Matthew Weier O'Phinney added a comment -

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.

Show
Matthew Weier O'Phinney added a comment - 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.
Hide
Matthew Weier O'Phinney added a comment -

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

Show
Matthew Weier O'Phinney added a comment - Added in trunk and 1.5 release branch; docs updated in trunk.
Hide
Wil Sinclair added a comment -

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.

Show
Wil Sinclair added a comment - 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.

People

Vote (0)
Watch (0)

Dates

  • Due:
    Created:
    Updated:
    Resolved:

Time Tracking

Estimated:
15m
Original Estimate - 15 minutes
Remaining:
15m
Remaining Estimate - 15 minutes
Logged:
Not Specified
Time Spent - Not Specified