diff --git a/src/Form.php b/src/Form.php index 960b7b92..7a2cb249 100644 --- a/src/Form.php +++ b/src/Form.php @@ -2,8 +2,8 @@ /** * Zend Framework (http://framework.zend.com/) * - * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) + * @link http://github.com/zendframework/zend-form for the canonical source repository + * @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -770,6 +770,13 @@ public function attachInputFilterDefaults(InputFilterInterface $inputFilter, Fie || ! $fieldset->getTargetElement() instanceof FieldsetInterface || $inputFilter instanceof CollectionInputFilter ) { + if ($fieldset === $this && $fieldset instanceof InputFilterProviderInterface) { + foreach ($fieldset->getInputFilterSpecification() as $name => $spec) { + $input = $inputFactory->createInput($spec); + $inputFilter->add($input, $name); + } + } + foreach ($elements as $name => $element) { if ($this->preferFormInputFilter && $inputFilter->has($name)) { continue; @@ -801,13 +808,6 @@ public function attachInputFilterDefaults(InputFilterInterface $inputFilter, Fie $inputFilter->add($input, $name); } } - - if ($fieldset === $this && $fieldset instanceof InputFilterProviderInterface) { - foreach ($fieldset->getInputFilterSpecification() as $name => $spec) { - $input = $inputFactory->createInput($spec); - $inputFilter->add($input, $name); - } - } } foreach ($fieldset->getFieldsets() as $name => $childFieldset) { diff --git a/test/FieldsetTest.php b/test/FieldsetTest.php index 2d5652bc..beb87765 100644 --- a/test/FieldsetTest.php +++ b/test/FieldsetTest.php @@ -2,8 +2,8 @@ /** * Zend Framework (http://framework.zend.com/) * - * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) + * @link http://github.com/zendframework/zend-form for the canonical source repository + * @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -572,4 +572,32 @@ public function testBindValuesPreservesNewValueAfterValidation() $this->assertSame('New value', $form->get('foo')->getValue()); } + + public function testInputFilterProviderInterfaceFieldsetCustomElementValidator() + { + $form = new Form(); + $form->add(new TestAsset\InputFilterProviderFieldset('fieldset')); + + $inputFilter = $form->getInputFilter(); + $fieldset = $inputFilter->get('fieldset'); + $input = $fieldset->get('custom'); + + $validators = $input->getValidatorChain()->getValidators(); + $this->assertCount(1, $validators); + $this->assertInstanceOf(TestAsset\CustomValidator::class, $validators[0]['instance']); + } + + public function testInputFilterProviderInterfaceFieldsetDefaultElementValidator() + { + $form = new Form(); + $form->add(new TestAsset\InputFilterProviderFieldset('fieldset')); + + $inputFilter = $form->getInputFilter(); + $fieldset = $inputFilter->get('fieldset'); + $input = $fieldset->get('default'); + + $validators = $input->getValidatorChain()->getValidators(); + $this->assertCount(1, $validators); + $this->assertInstanceOf(\Zend\Validator\Step::class, $validators[0]['instance']); + } } diff --git a/test/FormTest.php b/test/FormTest.php index 484b3e94..9c9a3ece 100644 --- a/test/FormTest.php +++ b/test/FormTest.php @@ -2,8 +2,8 @@ /** * Zend Framework (http://framework.zend.com/) * - * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) + * @link http://github.com/zendframework/zend-form for the canonical source repository + * @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -2264,4 +2264,28 @@ public function testShouldHydrateEmptyCollection() $this->assertTrue($this->form->isValid()); $this->assertEquals([], $this->form->getObject()->foo); } + + public function testInputFilterProviderInterfaceFormCustomElementValidator() + { + $form = new TestAsset\InputFilterProvider(); + + $inputFilter = $form->getInputFilter(); + $input = $inputFilter->get('custom'); + + $validators = $input->getValidatorChain()->getValidators(); + $this->assertCount(1, $validators); + $this->assertInstanceOf(TestAsset\CustomValidator::class, $validators[0]['instance']); + } + + public function testInputFilterProviderInterfaceFormDefaultElementValidator() + { + $form = new TestAsset\InputFilterProvider(); + + $inputFilter = $form->getInputFilter(); + $input = $inputFilter->get('default'); + + $validators = $input->getValidatorChain()->getValidators(); + $this->assertCount(1, $validators); + $this->assertInstanceOf(\Zend\Validator\Step::class, $validators[0]['instance']); + } } diff --git a/test/TestAsset/CustomValidator.php b/test/TestAsset/CustomValidator.php new file mode 100644 index 00000000..d7c6f3c5 --- /dev/null +++ b/test/TestAsset/CustomValidator.php @@ -0,0 +1,25 @@ + $this->getName(), + 'validators' => [ + ['name' => Step::class], + ], + ]; + } +} diff --git a/test/TestAsset/InputFilterProvider.php b/test/TestAsset/InputFilterProvider.php index c62af9f0..9490939b 100644 --- a/test/TestAsset/InputFilterProvider.php +++ b/test/TestAsset/InputFilterProvider.php @@ -2,8 +2,8 @@ /** * Zend Framework (http://framework.zend.com/) * - * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) + * @link http://github.com/zendframework/zend-form for the canonical source repository + * @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -21,9 +21,12 @@ public function __construct($name = null, $options = []) $this->add([ 'name' => 'foo', 'options' => [ - 'label' => 'Foo' + 'label' => 'Foo', ], ]); + + $this->add(new ElementWithStepValidator('custom')); + $this->add(new ElementWithStepValidator('default')); } public function getInputFilterSpecification() @@ -31,7 +34,12 @@ public function getInputFilterSpecification() return [ 'foo' => [ 'required' => true, - ] + ], + 'custom' => [ + 'validators' => [ + ['name' => CustomValidator::class], + ], + ], ]; } } diff --git a/test/TestAsset/InputFilterProviderFieldset.php b/test/TestAsset/InputFilterProviderFieldset.php index b06a8a03..2ceb2343 100644 --- a/test/TestAsset/InputFilterProviderFieldset.php +++ b/test/TestAsset/InputFilterProviderFieldset.php @@ -2,8 +2,8 @@ /** * Zend Framework (http://framework.zend.com/) * - * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) + * @link http://github.com/zendframework/zend-form for the canonical source repository + * @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -21,10 +21,13 @@ public function __construct($name = null, $options = []) $this->add([ 'name' => 'foo', 'options' => [ - 'label' => 'Foo' + 'label' => 'Foo', ], ]); + $this->add(new ElementWithStepValidator('custom')); + $this->add(new ElementWithStepValidator('default')); + $this->add(new BasicFieldset()); } @@ -33,7 +36,12 @@ public function getInputFilterSpecification() return [ 'foo' => [ 'required' => true, - ] + ], + 'custom' => [ + 'validators' => [ + ['name' => CustomValidator::class], + ], + ], ]; } }