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

Allow Forms to have an InputFilterSpecification #3162

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions library/Zend/Form/Form.php
Expand Up @@ -649,6 +649,14 @@ public function attachInputFilterDefaults(InputFilterInterface $inputFilter, Fie
{
$formFactory = $this->getFormFactory();
$inputFactory = $formFactory->getInputFilterFactory();

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

foreach ($fieldset->getElements() as $element) {
$name = $element->getName();

Expand Down
9 changes: 9 additions & 0 deletions tests/ZendTest/Form/FormTest.php
Expand Up @@ -173,6 +173,15 @@ public function testDefaultNonRequiredInputFilterIsSet()
$this->assertFalse($fooInput->isRequired());
}

public function testInputProviderInterfaceAddsInputFilters()
{
$form = new TestAsset\InputFilterProvider();

$inputFilter = $form->getInputFilter();
$fooInput = $inputFilter->get('foo');
$this->assertTrue($fooInput->isRequired());
}

public function testCallingIsValidRaisesExceptionIfNoDataSet()
{
$this->setExpectedException('Zend\Form\Exception\DomainException');
Expand Down
39 changes: 39 additions & 0 deletions tests/ZendTest/Form/TestAsset/InputFilterProvider.php
@@ -0,0 +1,39 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Form
*/

namespace ZendTest\Form\TestAsset;

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

class InputFilterProvider extends Form implements InputFilterProviderInterface
{
public function __construct($name = null, $options = array())
{
parent::__construct($name, $options);

$this->add(array(
'name' => 'foo',
'options' => array(
'label' => 'Foo'
),
));

}

public function getInputFilterSpecification()
{
return array(
'foo' => array(
'required' => true,
)
);
}
}