Skip to content

Commit

Permalink
Merge pull request zendframework#4372 from noopable/patch-FormElement…
Browse files Browse the repository at this point in the history
…Manager-retrieval

Ability to load custom form classes from FormElementManager in Mvc.
  • Loading branch information
weierophinney committed May 1, 2013
2 parents 64b8f94 + 69dc3de commit 048f6b0
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
11 changes: 11 additions & 0 deletions library/Zend/Mvc/Service/FormElementManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Zend\Mvc\Service;

use Zend\Form\Form;
use Zend\Form\FormElementManager;
use Zend\ServiceManager\ServiceLocatorInterface;

Expand All @@ -25,6 +26,16 @@ class FormElementManagerFactory extends AbstractPluginManagerFactory
public function createService(ServiceLocatorInterface $serviceLocator)
{
$plugins = parent::createService($serviceLocator);
if ($serviceLocator->has('Di')) {
$di = $serviceLocator->get('Di');
$im = $di->instanceManager();
if (!$im->getTypePreferences('Zend\Form\FormInterface')) {
$form = new Form;
$im->setTypePreference('Zend\Form\FormInterface', array($form));
}
$plugins->addPeeringServiceManager($serviceLocator);
$plugins->setRetrieveFromPeeringManagerFirst(true);
}
return $plugins;
}
}
73 changes: 73 additions & 0 deletions tests/ZendTest/Mvc/Service/FormElementManagerFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Mvc
*/

namespace ZendTest\Mvc\Service;

use ArrayObject;
use PHPUnit_Framework_TestCase as TestCase;
use Zend\Mvc\Service\FormElementManagerFactory;
use Zend\Mvc\Service\DiFactory;
use Zend\Mvc\Service\DiAbstractServiceFactoryFactory;
use Zend\Mvc\Service\DiServiceInitializerFactory;
use Zend\ServiceManager\Config;
use Zend\ServiceManager\ServiceManager;
use Zend\Mvc\Exception;
use Zend\Form\FormElementManager;

class FormElementManagerFactoryTest extends TestCase
{
/**
* @var ServiceManager
*/
protected $services;

/**
* @var \Zend\Mvc\Controller\ControllerManager
*/
protected $loader;

public function setUp()
{
$formElementManagerFactory = new FormElementManagerFactory();
$config = new ArrayObject(array('di' => array()));
$this->services = new ServiceManager();
$this->services->setService('Zend\ServiceManager\ServiceLocatorInterface', $this->services);
$this->services->setFactory('FormElementManager', $formElementManagerFactory);
$this->services->setService('Config', $config);
$this->services->setFactory('Di', new DiFactory());
$this->services->setFactory('DiAbstractServiceFactory', new DiAbstractServiceFactoryFactory());
$this->services->setFactory('DiServiceInitializer', new DiServiceInitializerFactory());
}

public function testWillGetFormElementManager()
{
$formElementManager = $this->services->get('FormElementManager');
$this->assertInstanceof('Zend\Form\FormElementManager', $formElementManager);
}

public function testWillInstantiateFormFromInvokable()
{
$formElementManager = $this->services->get('FormElementManager');
$form = $formElementManager->get('form');
$this->assertInstanceof('Zend\Form\Form', $form);
}

public function testWillInstantiateFormFromDiAbstractFactory()
{
//without DiAbstractFactory
$standaloneFormElementManager = new FormElementManager();
$this->assertFalse($standaloneFormElementManager->has('ZendTest\Mvc\Service\TestAsset\CustomForm'));
//with DiAbstractFactory
$formElementManager = $this->services->get('FormElementManager');
$this->assertTrue($formElementManager->has('ZendTest\Mvc\Service\TestAsset\CustomForm'));
$form = $formElementManager->get('ZendTest\Mvc\Service\TestAsset\CustomForm');
$this->assertInstanceof('ZendTest\Mvc\Service\TestAsset\CustomForm', $form);
}
}
32 changes: 32 additions & 0 deletions tests/ZendTest/Mvc/Service/TestAsset/CustomForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Form
*/

namespace ZendTest\Mvc\Service\TestAsset;

use Zend\Form\Form;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;

class CustomForm extends Form
{
public function __construct()
{
parent::__construct('test_form');

$this->setAttribute('method', 'post')
->setHydrator(new ClassMethodsHydrator());

$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit'
)
));
}
}

0 comments on commit 048f6b0

Please sign in to comment.