Last active
February 19, 2016 13:39
-
-
Save geerteltink/5dc68acbfe406b79cee3 to your computer and use it in GitHub Desktop.
Create a standalone zend-inputfilter with a ContainerInterop container.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require 'vendor/autoload.php'; | |
| use Xtreamwayz\HTMLFormValidator\Validator\RecaptchaValidator; | |
| use Zend\Filter\FilterPluginManager; | |
| use Zend\InputFilter\Factory; | |
| use Zend\InputFilter\InputFilterPluginManager; | |
| use Zend\ServiceManager\Config; | |
| use Zend\ServiceManager\ServiceManager; | |
| use Zend\Validator\ValidatorPluginManager; | |
| $config = [ | |
| 'zend-inputfilter' => [ | |
| 'validators' => [ | |
| // Attach custom validators or override standard validators | |
| 'invokables' => [ | |
| 'recaptcha' => RecaptchaValidator::class, | |
| ], | |
| ], | |
| 'filters' => [ | |
| // Attach custom filters or override standard filters | |
| 'invokables' => [ | |
| ], | |
| ], | |
| ], | |
| ]; | |
| // Build container | |
| $container = new ServiceManager($config); | |
| $container->setService('config', $config); | |
| // Build factory | |
| $factory = new Factory( | |
| new InputFilterPluginManager($container) | |
| ); | |
| $factory->getDefaultFilterChain()->setPluginManager( | |
| new FilterPluginManager($container, $config['zend-inputfilter']['filters']) | |
| ); | |
| $factory->getDefaultValidatorChain()->setPluginManager( | |
| new ValidatorPluginManager($container, $config['zend-inputfilter']['validators']) | |
| ); | |
| //$factory = new Factory(); | |
| $input = $factory->createInput(['name' => 'foo']); | |
| $input->getFilterChain() | |
| ->attachByName('stringtrim') | |
| ->attachByName('alpha'); | |
| //$input->getValidatorChain()->attachByName('recaptcha', ['key' => 'secret_key']); | |
| $inputFilter = $factory->createInputFilter([]); | |
| $inputFilter->add($input); | |
| $inputFilter->setData([ | |
| 'foo' => ' Bar3 ', | |
| ]); | |
| var_dump($factory->getDefaultValidatorChain()->getPluginManager()->has('recaptcha')); | |
| var_dump($input->getValidatorChain()->getPluginManager()->has('recaptcha')); | |
| var_dump($inputFilter->isValid()); | |
| var_dump($inputFilter->getValues()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment