zendframework/zf2 — pull request #4283 by wryck7 (2013-04-21 00:03:18+00:00)
Title: Added 'Zend\Form\FormManager' implementation

I opened an issue (#4228) to discuss some ideas I had, and now I have some code I wrote. This PR is supposed to provide the following:

- [x] Implementation of `Zend\Form\FormManager`:
This class will manage anything related to `Zend\Form`, like `FormElementManager`, `FormFactory`, `InputFilterFactory` and possibly `FormObjectManager` - see #4100.

- [x] Creation of `Zend\Form\FormFactory` to replace `Zend\Form\Factory`:
The intention of this is that the factory would use `FormManager` to retrieve its dependencies. The `Zend\Form\Factory` would be deprecated.

- [ ] Create a `ModuleManager` listener:
We would deprecate `form_elements` key and `getFormElementConfig` method and provide a `form_manager` key (similar with the one presented by @stephanoff in #4100), and a method `getFormConfig()`.

```PHP
public function getFormConfig()
{
return array(
'forms' => ( /* Especifications for Zend\Form\FormAbstractServiceFactory */ ),
'element_manager' => (),
'object_manager' => (), // plz refer to #4100
);
}
```
Or in a config file:

```PHP
return array(
'form_manager' => array(
'forms' => (),
'elements' => (), // instead of 'element_manager'
'objects' => (), // instead of 'object_manager'
),
);
```

Something like this, let me know if you have suggestions.

- [ ] Implementation of a `Zend\EventManager` integration:
This integration would be added as a **soft** dependency that could be activated via a key in the configuration (similar with the one presented by @DASPRiD in #4187):

```PHP
return array(
'form_manager' => array(
'event_manager_enabled' => true,
),
);
```
The idea of this integration is to provide something like the following examples:

```PHP
// In a 'product' fieldset
$this->add(array(
'name' => 'isSale',
'type' => 'Checkbox',
));

$this->add(array(
'name' => 'salePrice',
'event_listeners' => array( // array of events to be attached to the EventManager
array(
'event' => 'validate', // Would be triggered when isValid() gets called
'callback' => function ($e) {
$form = $e->getForm();

// If 'isSale' is NOT checked 'salePrice' should not be requiered
if (!$form->get('product')->get('isSale')->isChecked()) {
$form->getInputFilter()->get('product')->get('salePrice')->setRequired(false);
}
},
'priority' => 25,
),
array(
'event' => 'prepare', // Would be triggered when prepare() gets called
'callback' => function ($e) {
$form = $e->getForm();

if (!$form->get('product')->get('isSale')->isChecked()) {
// This could be done in the 'validate' event, but it's here just for demonstration
$form->get('product')->get('salePrice')->setAttribute('disabled', 'disabled');
}
},
),
),
));
```