On 11/04/2013, at 10:24 AM, David Muir <[email protected]> wrote:
> On 18/03/13 22:51, David Muir wrote:
>> I've just set up a login form using the
>> Zend\Authorization\Validator\Authorization.
>> Overall things are working reasonably well, but there are few
>> annoyances/oddities.
>>
>> First, to load the validator, I have to reference the full classname
>> "Zend\Authorization\Validator\Authorization" instead of a short name like
>> you can with the standard validators (e.g. "EmailAddress" instead of
>> "Zend\Validator\EmailAddress").
>
> Sorry, Authentication, not Authorization
> Figured out that it is not registered by default with the validator plugin
> manager. I tried adding to my module.config.php:
>
> 'validators' => array(
> 'invocables' => array(
> 'Authentication' => 'Zend\Authentication\Validator\Authentication'
> ),
> ),
>
> But I still get a ServiceNotFoundException.
>
> I'm also wanting to use some custom validators, but I can't get them working
> either.
> I've tried moving the config to Module.php using ValidatorProviderInterface
> and getValidatorConfig() and still no go. Tried using a factory too, and it
> seems the factory never gets called. Very frustrating since the docs only
> show how to create validators, but not set them up with the
> ValidatorPluginManager.
>
Sorry for all the replies to myself, but this is a problem I've seen several
other people ask about without getting clear answers.
So firstly, it seems that when you follow the ZF2 user guide and create an
InputFilter the way it describes, we're not getting the application's Validator
Plugin Manager. Instead we're getting a stand-alone instance that only knows
about the validators under Zend\Validator and Zend\I18n\Validator.
So the question I now have is, what's the best way to create InputFilters that
are config aware?
What I ended up doing is adding an InputFilterFactory factory in my module's
services config:
'InputFilterFactory' => function($sm){
$validators = $sm->get('ValidatorManager');
$filters = $sm->get('FilterManager');
$validatorChain = new \Zend\Validator\ValidatorChain();
$validatorChain->setPluginManager($validators);
$filterChain = new \Zend\Filter\FilterChain();
$filterChain->setPluginManager($filters);
$factory = new \Zend\InputFilter\Factory();
$factory->setDefaultValidatorChain($validatorChain);
$factory->setDefaultFilterChain($filterChain);
return $factory;
},
I then pass this factory to all my InputFilter instances so they can be
constructed:
'LoginInputFilter' => function ($sm){
$adapter = $sm->get('AuthenticationAdapter');
$service = $sm->get('AuthenticationService');
$factory = $sm->get('InputFilterFactory');
$loginFilter = new LoginInputFilter($factory, $service,
$adapter);
return $loginFilter;
}
It doesn't feel quite right, but I don't see any other clear way to deal with
it.
Cheers,
David