Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Allow Forms to have an InputFilterSpecification #3162

Closed
wants to merge 3 commits into from

3 participants

@Mezzle

No description provided.

@weierophinney
@Mezzle

@weierophinney please see

https://gist.github.com/dfee3d6b21d6443d95f0

In this example - the form will come back valid, even though there is an InputFilterSpecification to say that name should not be empty. This is because the getInputFilter only looks at the sub-elements of the form for an InputFilterSpecification, not the form itself. This patch makes it look at the form itself.

@bakura10

+1 on this one.

@weierophinney it was never supported. I already tried to add support for this but obviously I just miss the most easy way to fix it ;-).

library/Zend/Form/Form.php
@@ -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) {

Please "as" instead of "AS".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@bakura10

@Mezzle can you add a test case please ?

@weierophinney

@Mezzle Looks good. As @bakura10 notes... I need a test case. :)

@weierophinney

@Mezzle Pinging again -- if you want this in 2.1, we need a test case soon; 2.1 drops beginning of January.

@Mezzle

I'll get on it when I have a few minutes spare.

@Mezzle

Rebased and test added.

@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/3162'
Close #3162
1b24cb6
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/3162' into develop
Forward port #3162
d3d9f6c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
8 library/Zend/Form/Form.php
@@ -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();
View
9 tests/ZendTest/Form/FormTest.php
@@ -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');
View
39 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,
+ )
+ );
+ }
+}
Something went wrong with that request. Please try again.