Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 5b53d63

Browse files
committed
Merge branch 'hotfix/45' into develop
Forward port #45
2 parents fc7d297 + 05b4b9e commit 5b53d63

File tree

5 files changed

+121
-2
lines changed

5 files changed

+121
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ All notable changes to this project will be documented in this file, in reverse
4444
- [#38](https://github.com/zendframework/zend-form/pull/38) removes the
4545
arbitrary restriction of only the "labelledby" and "describedby" aria
4646
attributes on form element view helpers; any aria attribute is now allowed.
47+
- [#45](https://github.com/zendframework/zend-form/pull/45) fixes the behavior
48+
in `Zend\Form\Factory::create()` when pulling elements from the form element
49+
manager; it now will pass specifications provided for the given element when
50+
calling the manager's `get()` method.
4751

4852
## 2.7.0 - 2016-02-22
4953

src/Factory.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,12 @@ public function create($spec)
108108
$spec = $this->validateSpecification($spec, __METHOD__);
109109
$type = isset($spec['type']) ? $spec['type'] : Element::class;
110110

111-
$element = $this->getFormElementManager()->get($type);
111+
$creationOptions = [];
112+
if (isset($spec['options']) && is_array($spec['options'])) {
113+
$creationOptions = $spec['options'];
114+
}
115+
116+
$element = $this->getFormElementManager()->get($type, $creationOptions);
112117

113118
if ($element instanceof FormInterface) {
114119
return $this->configureForm($element, $spec);

test/FactoryTest.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
namespace ZendTest\Form;
1111

1212
use PHPUnit_Framework_TestCase as TestCase;
13+
use DateTime;
1314
use Zend\Filter;
1415
use Zend\Form;
15-
use Zend\Form\FormElementManager;
1616
use Zend\Form\Factory as FormFactory;
17+
use Zend\Form\FormElementManager;
18+
use Zend\Form\FormInterface;
1719
use Zend\ServiceManager\ServiceManager;
1820
use Zend\Hydrator\HydratorPluginManager;
1921

@@ -755,4 +757,30 @@ public function testCanCreateFormWithNullElements()
755757
$this->assertFalse($form->has('baz'));
756758
$this->assertTrue($form->has('bat'));
757759
}
760+
761+
public function testOptionsArePassedAsCreationOptionsToFactories()
762+
{
763+
$formManager = $this->factory->getFormElementManager();
764+
$formManager->setFactory('customCreatedForm', TestAsset\CustomCreatedFormFactory::class);
765+
766+
/* @var $form TestAsset\CustomCreatedForm */
767+
$form = $this->factory->create([
768+
'name' => 'some_name',
769+
'type' => 'customCreatedForm',
770+
'options' => [
771+
'created' => '2016-02-19',
772+
'some_other_option' => 1234
773+
]
774+
]);
775+
776+
$this->assertInstanceOf(FormInterface::class, $form);
777+
$this->assertInstanceOf(TestAsset\CustomCreatedForm::class, $form);
778+
$this->assertSame('some_name', $form->getName());
779+
$this->assertSame(1234, $form->getOption('some_other_option'));
780+
781+
/* @var $created DateTime */
782+
$created = $form->getCreated();
783+
$this->assertInstanceOf(DateTime::class, $created);
784+
$this->assertSame('2016-02-19', $created->format('Y-m-d'));
785+
}
758786
}

test/TestAsset/CustomCreatedForm.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/**
4+
* Zend Framework (http://framework.zend.com/)
5+
*
6+
* @link http://github.com/zendframework/zf2 for the canonical source repository
7+
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
8+
* @license http://framework.zend.com/license/new-bsd New BSD License
9+
*/
10+
11+
namespace ZendTest\Form\TestAsset;
12+
13+
use Zend\Form\Form;
14+
use DateTime;
15+
16+
class CustomCreatedForm extends Form
17+
{
18+
private $created;
19+
20+
public function __construct(DateTime $created, $name = null, $options = array())
21+
{
22+
parent::__construct($name, $options);
23+
$this->created = $created;
24+
}
25+
26+
public function getCreated()
27+
{
28+
return $this->created;
29+
}
30+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace ZendTest\Form\TestAsset;
11+
12+
use Interop\Container\ContainerInterface;
13+
use Zend\ServiceManager\FactoryInterface;
14+
use Zend\ServiceManager\ServiceLocatorInterface;
15+
use DateTime;
16+
17+
class CustomCreatedFormFactory implements FactoryInterface
18+
{
19+
private $creationOptions = [];
20+
21+
public function __invoke(ContainerInterface $container, $name, array $options = null)
22+
{
23+
$options = $options ?: [];
24+
$creationString = 'now';
25+
26+
if (isset($options['created'])) {
27+
$creationString = $options['created'];
28+
unset($options['created']);
29+
}
30+
31+
$created = new DateTime($creationString);
32+
33+
$name = null;
34+
if (isset($options['name'])) {
35+
$name = $options['name'];
36+
unset($options['name']);
37+
}
38+
39+
$form = new CustomCreatedForm($created, $name, $options);
40+
return $form;
41+
}
42+
43+
public function createService(ServiceLocatorInterface $container)
44+
{
45+
return $this($container, CustomCreatedForm::class, $this->creationOptions);
46+
}
47+
48+
public function setCreationOptions(array $options)
49+
{
50+
$this->creationOptions = $options;
51+
}
52+
}

0 commit comments

Comments
 (0)