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

Form implements InputFilterProviderInterface - validators fix #82

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
32 changes: 30 additions & 2 deletions test/FieldsetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand Down Expand Up @@ -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']);
}
}
28 changes: 26 additions & 2 deletions test/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand Down Expand Up @@ -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']);
}
}
25 changes: 25 additions & 0 deletions test/TestAsset/CustomValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zend-form for the canonical source repository
* @copyright Copyright (c) 2017 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\Validator\ValidatorInterface;

class CustomValidator implements ValidatorInterface
{
public function isValid($value)
{
return true;
}

public function getMessages()
{
return [];
}
}
27 changes: 27 additions & 0 deletions test/TestAsset/ElementWithStepValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zend-form for the canonical source repository
* @copyright Copyright (c) 2017 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\Form\Element;
use Zend\InputFilter\InputProviderInterface;
use Zend\Validator\Step;

class ElementWithStepValidator extends Element implements InputProviderInterface
{
public function getInputSpecification()
{
return [
'name' => $this->getName(),
'validators' => [
['name' => Step::class],
],
];
}
}
16 changes: 12 additions & 4 deletions test/TestAsset/InputFilterProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand All @@ -21,17 +21,25 @@ 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()
{
return [
'foo' => [
'required' => true,
]
],
'custom' => [
'validators' => [
['name' => CustomValidator::class],
],
],
];
}
}
16 changes: 12 additions & 4 deletions test/TestAsset/InputFilterProviderFieldset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand All @@ -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());
}

Expand All @@ -33,7 +36,12 @@ public function getInputFilterSpecification()
return [
'foo' => [
'required' => true,
]
],
'custom' => [
'validators' => [
['name' => CustomValidator::class],
],
],
];
}
}