Quantcast

Abstract Factory for custom Validators - options discarded?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Abstract Factory for custom Validators - options discarded?

Saeven
My goal is to pass custom options to validators, as is done by the ZF2-provided validators.  Consider this validator config:

'filters' => array(
                'leaderboard'  => array(
                    'required' => true,
                    'filters'  => array(
                        array('name' => 'stringtrim'),
                    ),
                    'validators' => array(
                        array(
                            'name'    => '\LDP\Form\Validator\UniqueAtom',
                            'options' => array(
                                'key' => 'foo',
                            ),
                        ),
                    ),
                ),
            ),

In this case, my validator is provided by an abstract factory which is specified in my application's getValidatorConfig().  It seems though, judging by lines 95+ in AbstractPluginManager that this function sequence ignores creation options:

public function get($name, $options = array(), $usePeeringServiceManagers = true)
    {
        // Allow specifying a class name directly; registers as an invokable class
        if (!$this->has($name) && $this->autoAddInvokableClass && class_exists($name)) {
            $this->setInvokableClass($name, $name);
        }

        $this->creationOptions = $options;
        $instance = parent::get($name, $usePeeringServiceManagers);
        $this->creationOptions = null;
        $this->validatePlugin($instance);
        return $instance;
    }

In short, the creation options make their way there, but they're never ferried about.  What's the best solution?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Abstract Factory for custom Validators - options discarded?

Saeven
I've just found the solution in the source. You have to make your factory implement Zend\ServiceManager\MutableCreationOptionsInterface

You can then use whatever it passes to instantiate the "next thing". Feels like an official band-aid hehe, but it works.
Loading...