Implementing a form factory
The purpose of a form factory is to generate a usable form object from a single configuration array. The form object should have the ability to retrieve the individual elements it contains so that output can be generated.
How to do it...
First, let's create a class called
Application\Form\Factory
to contain the factory code. It will have only one property,$elements
, with a getter:namespace Application\Form; class Factory { protected $elements; public function getElements() { return $this->elements; } // remaining code }
Before we define the primary form generation method, it's important to consider what configuration format we plan to receive, and what exactly the form generation will produce. For this illustration, we will assume that the generation will produce a
Factory
instance, with an$elements
property. This property would be an array ofApplication\Form\Generic
orApplication\Form\Element
classes.We are now ready to tackle the
generate...