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

Add element input filters before form input filters #5479

Closed
wants to merge 5 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions library/Zend/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 ?
*
Expand Down Expand Up @@ -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
Copy link
Member

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?

// if self::$preferFormInputFilter default value is set to false
if (false === $this->hasSetPreferFormInputFilter) {
$this->preferFormInputFilter = false;
}

return $this;
}

Expand Down Expand Up @@ -712,6 +727,7 @@ public function useInputFilterDefaults()
public function setPreferFormInputFilter($preferFormInputFilter)
{
$this->preferFormInputFilter = (bool) $preferFormInputFilter;
$this->hasSetPreferFormInputFilter = true;
return $this;
}

Expand Down Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with this approach is that replace is not in the interface. If the input filter implementation used does not have that method, we end up getting an error.

Two options:

  1. Duck-type: if (method_exists($inputFilter, 'replace'))
  2. Create a ReplaceableInputInterface with the replace method, and have the BaseInputFilter implement it. Then do an instanceof check: if ($inputFilter instanceof ReplaceableInputInterface).

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);
}

Expand Down
38 changes: 37 additions & 1 deletion library/Zend/InputFilter/BaseInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
* @todo How should we deal with required input when data is missing?
* should a message be returned? if so, what message?
*/
class BaseInputFilter implements InputFilterInterface, UnknownInputsCapableInterface, InitializableInterface
class BaseInputFilter implements
InputFilterInterface,
UnknownInputsCapableInterface,
InitializableInterface,
ReplaceableInputInterface
{
protected $data;
protected $inputs = array();
Expand Down Expand Up @@ -84,6 +88,38 @@ public function add($input, $name = null)
return $this;
}

/**
* Replace a named input
*
* @param InputInterface|InputFilterInterface $input
* @param string $name Name of the input to replace
* @throws Exception\InvalidArgumentException
* @return self
*/
public function replace($input, $name)
{
if (!$input instanceof InputInterface && !$input instanceof InputFilterInterface) {
throw new Exception\InvalidArgumentException(sprintf(
'%s expects an instance of %s or %s as its first argument; received "%s"',
__METHOD__,
'Zend\InputFilter\InputInterface',
'Zend\InputFilter\InputFilterInterface',
(is_object($input) ? get_class($input) : gettype($input))
));
}

if (!array_key_exists($name, $this->inputs)) {
throw new Exception\InvalidArgumentException(sprintf(
'%s: no input found matching "%s"',
__METHOD__,
$name
));
}

$this->inputs[$name] = $input;
return $this;
}

/**
* Retrieve a named input
*
Expand Down
15 changes: 15 additions & 0 deletions library/Zend/InputFilter/ReplaceableInputInterface.php
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
Copy link
Member

Choose a reason for hiding this comment

The 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document the method with a docblock

}
36 changes: 36 additions & 0 deletions tests/ZendTest/Form/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import this classes as use statements

$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());
}
}