On Sun, Mar 31, 2013 at 10:42 PM, rodrigo_00 <[email protected]> wrote:
> Hi :) i have this in my module.config.php:
>
> 'validators' => array(
> 'factories' => array(
> 'Application\Validator\SecretCode' => function($sm) {
> $validator = new \Application\Validator\SecretCode;
>
> $validator->setUserMapper($sm->get('Application\Mapper\User'));
> return $validator;
> },
> )
> ),
>
> But the validator is never created via the factory, so the mapper is never
> injected, what am I doing wrong?
Plugin managers are scoped, to prevent collisions with each other and
with the application service manager. Thus, if you need to access
application-level services, such as your mapper, you will need to
retrieve the application service manager. You do this using the
"getServiceLocator()" method of the plugin manager passed in to the
factory.
For this reason, I also recommend naming the plugin manager argument
semantically, so that you remember what it represents.
Also, plugins -- such as validators, filters, view helpers, etc. --
should be given a _short_ name, and not the fully qualified class
name, as the service name.
I'd rewrite your example above as follows:
'validators' => array(
'factories' => array(
'SecretCode' => function ($validators) {
$validator = new \Application\Validator\SecretCode();
$services = $validators->getServiceLocator();
$validator->setUserMapper($services->get('Application\Mapper\User');
return $validator;
}
),
)
--
Matthew Weier O'Phinney
Project Lead | [email protected]
Zend Framework | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc
--
List: [email protected]
Info: http://framework.zend.com/archives
Unsubscribe: [email protected]