Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

allow usage of validator and filter plugin managers in input filter factory if form manager injected #4851

Closed
wants to merge 4 commits into from

3 participants

@richardjennings

fix for issue #4801

Allow usage of Validator and Filter plugin managers in Input Filter factory if Form Manager Aware

@samsonasik

please provide unit test.

@weierophinney

@richardjennings Does #4788 fix the issues you encountered as well?

@richardjennings

@weierophinney No. Inputs with an undefined type are created with a clone of the InputFilter Factory validator/filter chains which do not have access to plugin managers.

I have provided an alternative fix, setting the plugin managers on the validator/filter chains if an InputFilterManager is provided with access to a ServiceLocatorInterface instance.

@weierophinney

@richardjennings Okay -- can I get some unit tests from you, then, please?

@richardjennings richardjennings check for servicelocator instance when inputfilterpluginmanager set a…
…nd inject validator and filter plugin managers into factory default filter and validator chains. Test setInputFilterPluginManager with and without ServiceLocator
1b26a41
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-inputfilter
@weierophinney weierophinney Merge pull request zendframework/zf2#4851 from trueaxiom/issue4801
allow usage of validator and filter plugin managers in input filter factory if form manager injected

Conflicts:
	tests/ZendTest/InputFilter/FactoryTest.php
2b8fd51
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-inputfilter
@weierophinney weierophinney Merge branch 'hotfix/4851' e059c77
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-inputfilter
@weierophinney weierophinney Merge branch 'hotfix/4851' into develop 5f9f6fb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 18, 2013
  1. @richardjennings
Commits on Jul 19, 2013
  1. @richardjennings
Commits on Jul 20, 2013
  1. @richardjennings

    if inputFilterManager set and has servicelocator, use servicelocator …

    richardjennings authored
    …to add plugin managers to validator and filter chains
Commits on Jul 24, 2013
  1. @richardjennings

    check for servicelocator instance when inputfilterpluginmanager set a…

    richardjennings authored
    …nd inject validator and filter plugin managers into factory default filter and validator chains. Test setInputFilterPluginManager with and without ServiceLocator
This page is out of date. Refresh to see the latest.
View
58 library/Zend/Form/FormElementManager.php
@@ -123,4 +123,62 @@ public function validatePlugin($plugin)
(is_object($plugin) ? get_class($plugin) : gettype($plugin))
));
}
+
+ /**
+ * Retrieve a service from the manager by name
+ *
+ * Allows passing an array of options to use when creating the instance.
+ * createFromInvokable() will use these and pass them to the instance
+ * constructor if not null and a non-empty array.
+ *
+ * @param string $name
+ * @param string|array $options
+ * @param bool $usePeeringServiceManagers
+ * @return object
+ */
+ public function get($name, $options = array(), $usePeeringServiceManagers = true)
+ {
+ if (is_string($options)) {
+ $options = array('name' => $options);
+ }
+ return parent::get($name, $options, $usePeeringServiceManagers);
+ }
+
+ /**
+ * Attempt to create an instance via an invokable class
+ *
+ * Overrides parent implementation by passing $creationOptions to the
+ * constructor, if non-null.
+ *
+ * @param string $canonicalName
+ * @param string $requestedName
+ * @return null|\stdClass
+ * @throws Exception\ServiceNotCreatedException If resolved class does not exist
+ */
+ protected function createFromInvokable($canonicalName, $requestedName)
+ {
+ $invokable = $this->invokableClasses[$canonicalName];
+
+ if (null === $this->creationOptions
+ || (is_array($this->creationOptions) && empty($this->creationOptions))
+ ) {
+ $instance = new $invokable();
+ } else {
+ if (isset($this->creationOptions['name'])) {
+ $name = $this->creationOptions['name'];
+ } else {
+ $name = $requestedName;
+ }
+
+ if (isset($this->creationOptions['options'])) {
+ $options = $this->creationOptions['options'];
+ } else {
+ $options = $this->creationOptions;
+ }
+
+ $instance = new $invokable($name, $options);
+ }
+
+ return $instance;
+ }
}
View
11 library/Zend/InputFilter/Factory.php
@@ -14,6 +14,7 @@
use Zend\Stdlib\ArrayUtils;
use Zend\Validator\ValidatorInterface;
use Zend\Validator\ValidatorChain;
+use Zend\ServiceManager\ServiceLocatorInterface;
class Factory
{
@@ -116,7 +117,15 @@ public function clearDefaultValidatorChain()
public function setInputFilterManager(InputFilterPluginManager $inputFilterManager)
{
$this->inputFilterManager = $inputFilterManager;
-
+ $serviceLocator = $this->inputFilterManager->getServiceLocator();
+ if ($serviceLocator && $serviceLocator instanceof ServiceLocatorInterface) {
+ if ($serviceLocator->has('ValidatorManager')) {
+ $this->getDefaultValidatorChain()->setPluginManager($serviceLocator->get('ValidatorManager'));
+ }
+ if ($serviceLocator->has('FilterManager')) {
+ $this->getDefaultFilterChain()->setPluginManager($serviceLocator->get('FilterManager'));
+ }
+ }
return $this;
}
View
29 tests/ZendTest/InputFilter/FactoryTest.php
@@ -16,6 +16,8 @@
use Zend\InputFilter\Input;
use Zend\InputFilter\InputFilter;
use Zend\Validator;
+use Zend\InputFilter\InputFilterPluginManager;
+use Zend\ServiceManager;
class FactoryTest extends TestCase
{
@@ -515,4 +517,31 @@ public function testConflictNameWithInputFilterType()
$this->assertInstanceOf('Zend\InputFilter\InputFilter', $inputFilter);
$this->assertTrue($inputFilter->has('type'));
}
+
+ public function testSetInputFilterManagerWithServiceManager()
+ {
+ $inputFilterManager = new InputFilterPluginManager;
+ $serviceManager = new ServiceManager\ServiceManager;
+ $serviceManager->setService('ValidatorManager', new Validator\ValidatorPluginManager);
+ $serviceManager->setService('FilterManager', new Filter\FilterPluginManager);
+ $inputFilterManager->setServiceLocator($serviceManager);
+ $factory = new Factory();
+ $factory->setInputFilterManager($inputFilterManager);
+ $this->assertInstanceOf(
+ 'Zend\Validator\ValidatorPluginManager',
+ $factory->getDefaultValidatorChain()->getPluginManager()
+ );
+ $this->assertInstanceOf(
+ 'Zend\Filter\FilterPluginManager',
+ $factory->getDefaultFilterChain()->getPluginManager()
+ );
+ }
+
+ public function testSetInputFilterManagerWithoutServiceManager()
+ {
+ $inputFilterManager = new InputFilterPluginManager();
+ $factory = new Factory();
+ $factory->setInputFilterManager($inputFilterManager);
+ $this->assertSame($inputFilterManager, $factory->getInputFilterManager());
+ }
}
Something went wrong with that request. Please try again.