-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Add element input filters before form input filters #5479
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
use Zend\InputFilter\InputFilterInterface; | ||
use Zend\InputFilter\InputFilterProviderInterface; | ||
use Zend\InputFilter\InputProviderInterface; | ||
use Zend\InputFilter\ReplaceableInputInterface; | ||
use Zend\Stdlib\ArrayUtils; | ||
use Zend\Stdlib\Hydrator\HydratorInterface; | ||
|
||
|
@@ -108,6 +109,13 @@ class Form extends Fieldset implements FormInterface | |
*/ | ||
protected $preferFormInputFilter = true; | ||
|
||
/** | ||
* Has preferFormInputFilter been set with setPreferFormInputFilter? | ||
* | ||
* @var bool | ||
*/ | ||
protected $hasSetPreferFormInputFilter = false; | ||
|
||
/** | ||
* Are the form elements/fieldsets wrapped by the form name ? | ||
* | ||
|
@@ -643,6 +651,13 @@ public function setInputFilter(InputFilterInterface $inputFilter) | |
$this->hasValidated = false; | ||
$this->hasAddedInputFilterDefaults = false; | ||
$this->filter = $inputFilter; | ||
|
||
// TODO: Remove the statement below and all self::$hasSetPreferFormInputFilter occurrences | ||
// if self::$preferFormInputFilter default value is set to false | ||
if (false === $this->hasSetPreferFormInputFilter) { | ||
$this->preferFormInputFilter = false; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
|
@@ -712,6 +727,7 @@ public function useInputFilterDefaults() | |
public function setPreferFormInputFilter($preferFormInputFilter) | ||
{ | ||
$this->preferFormInputFilter = (bool) $preferFormInputFilter; | ||
$this->hasSetPreferFormInputFilter = true; | ||
return $this; | ||
} | ||
|
||
|
@@ -756,13 +772,20 @@ public function attachInputFilterDefaults(InputFilterInterface $inputFilter, Fie | |
continue; | ||
} | ||
// Create a new empty default input for this element | ||
$spec = array('name' => $name, 'required' => false); | ||
$spec = array('name' => $name, 'required' => false); | ||
$input = $inputFactory->createInput($spec); | ||
} else { | ||
// Create an input based on the specification returned from the element | ||
$spec = $element->getInputSpecification(); | ||
$input = $inputFactory->createInput($spec); | ||
|
||
if ($inputFilter->has($name) && $inputFilter instanceof ReplaceableInputInterface) { | ||
$input->merge($inputFilter->get($name)); | ||
$inputFilter->replace($input, $name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem with this approach is that Two options:
The second is cleaner from an OOP perspective, and will ensure we include the functionality directly in the base interface in v3. |
||
continue; | ||
} | ||
} | ||
|
||
$input = $inputFactory->createInput($spec); | ||
$inputFilter->add($input, $name); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?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 Zend\InputFilter; | ||
|
||
interface ReplaceableInputInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please a docblock describing the purpose of the interface |
||
{ | ||
public function replace($input, $name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Document the method with a docblock |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1727,4 +1727,40 @@ public function testInputFilterNotAddedTwiceWhenUsingFieldsets() | |
$filters = $form->getInputFilter()->get('fieldset')->get('foo')->getFilterChain(); | ||
$this->assertEquals(1, $filters->count()); | ||
} | ||
|
||
public function testFormElementValidatorsMergeIntoAppliedInputFilter() | ||
{ | ||
$this->form->add(array( | ||
'name' => 'importance', | ||
'type' => 'Zend\Form\Element\Select', | ||
'options' => array( | ||
'label' => 'Importance', | ||
'empty_option' => '', | ||
'value_options' => array( | ||
'normal' => 'Normal', | ||
'important' => 'Important' | ||
), | ||
), | ||
)); | ||
|
||
$inputFilter = new \Zend\InputFilter\BaseInputFilter(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. import this classes as |
||
$factory = new \Zend\InputFilter\Factory(); | ||
$inputFilter->add($factory->createInput(array( | ||
'name' => 'importance', | ||
'required' => false, | ||
))); | ||
|
||
$data = array( | ||
'importance' => 'unimporant' | ||
); | ||
|
||
$this->form->setInputFilter($inputFilter); | ||
$this->form->setData($data); | ||
$this->assertFalse($this->form->isValid()); | ||
|
||
$data = array(); | ||
|
||
$this->form->setData($data); | ||
$this->assertTrue($this->form->isValid()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this TODO be completed?