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

Commit

Permalink
Merge branch 'hotfix/4996'
Browse files Browse the repository at this point in the history
Close #4996
  • Loading branch information
weierophinney committed Aug 21, 2013
2 parents 85829ec + 65b5c6a commit 24970da
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 9 deletions.
14 changes: 7 additions & 7 deletions library/Zend/Form/Form.php
Expand Up @@ -717,13 +717,6 @@ public function attachInputFilterDefaults(InputFilterInterface $inputFilter, Fie
$formFactory = $this->getFormFactory();
$inputFactory = $formFactory->getInputFilterFactory();

if ($fieldset === $this && $fieldset instanceof InputFilterProviderInterface) {
foreach ($fieldset->getInputFilterSpecification() as $name => $spec) {
$input = $inputFactory->createInput($spec);
$inputFilter->add($input, $name);
}
}

if ($fieldset instanceof Collection && $fieldset->getTargetElement() instanceof FieldsetInterface) {
$elements = $fieldset->getTargetElement()->getElements();
} else {
Expand Down Expand Up @@ -752,6 +745,13 @@ public function attachInputFilterDefaults(InputFilterInterface $inputFilter, Fie
$input = $inputFactory->createInput($spec);
$inputFilter->add($input, $name);
}

if ($fieldset instanceof InputFilterProviderInterface) {
foreach ($fieldset->getInputFilterSpecification() as $name => $spec) {
$input = $inputFactory->createInput($spec);
$inputFilter->add($input, $name);
}
}
}

foreach ($fieldset->getFieldsets() as $childFieldset) {
Expand Down
7 changes: 5 additions & 2 deletions library/Zend/InputFilter/BaseInputFilter.php
Expand Up @@ -73,9 +73,12 @@ public function add($input, $name = null)
}

if (isset($this->inputs[$name]) && $this->inputs[$name] instanceof InputInterface) {
// The element already exists, so merge the config. Please note that the order is important (already existing
// The element already exists, so merge the config. Please note
// that the order is important (already existing
// input is merged with the parameter given)
$input->merge($this->inputs[$name]);
$original = $this->inputs[$name];
$original->merge($input);
return $this;
}

$this->inputs[$name] = $input;
Expand Down
14 changes: 14 additions & 0 deletions tests/ZendTest/Form/FormTest.php
Expand Up @@ -1542,4 +1542,18 @@ public function testNestedFormElementNameWrapping()
$rootForm->prepare();
$this->assertEquals('form[element]', $element->getName());
}

/**
* @group 4996
*/
public function testCanOverrideDefaultInputSettings()
{
$myFieldset = new TestAsset\MyFieldset();
$myFieldset->setUseAsBaseFieldset(true);
$form = new Form();
$form->add($myFieldset);

$inputFilter = $form->getInputFilter()->get('my-fieldset');
$this->assertFalse($inputFilter->get('email')->isRequired());
}
}
34 changes: 34 additions & 0 deletions tests/ZendTest/Form/TestAsset/MyFieldset.php
@@ -0,0 +1,34 @@
<?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
*/

namespace ZendTest\Form\TestAsset;

use Zend\InputFilter\InputFilterProviderInterface;
use Zend\Form\Fieldset;

class MyFieldset extends Fieldset implements InputFilterProviderInterface
{
public function __construct()
{
parent::__construct('my-fieldset');
$this->add(array(
'type' => 'Email',
'name' => 'email',
));
}

public function getInputFilterSpecification()
{
return array(
'email' => array(
'required' => false,
),
);
}
}
18 changes: 18 additions & 0 deletions tests/ZendTest/InputFilter/BaseInputFilterTest.php
Expand Up @@ -765,4 +765,22 @@ public function testGetInputs()
$this->assertEquals('foo', $filters['foo']->getName());
$this->assertEquals('bar', $filters['bar']->getName());
}

/**
* @group 4996
*/
public function testAddingExistingInputWillMergeIntoExisting()
{
$filter = new InputFilter();

$foo1 = new Input('foo');
$foo1->setRequired(true);
$filter->add($foo1);

$foo2 = new Input('foo');
$foo2->setRequired(false);
$filter->add($foo2);

$this->assertFalse($filter->get('foo')->isRequired());
}
}

0 comments on commit 24970da

Please sign in to comment.