Zend Framework 2:如何在验证类中获取ServiceLocator [英] Zend Framework 2: How to get ServiceLocator in validation class

查看:101
本文介绍了Zend Framework 2:如何在验证类中获取ServiceLocator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的验证类中获取ServiceLocator.我尝试从Controller实例获取它,但它返回null.

I would like to get ServiceLocator in my validation class. I tried get it from Controller instance but it returns null.

MyValidation.php  
namespace Register\Validator;

use Zend\Validator\AbstractValidator;
use Register\Controller\RegisterController;

class MyValidation extends AbstractValidator {

    /*
    code...
    */

    function isValid($value)
    {
        $controller = new RegisterController();
        $sm = $controller->getServiceLocator();
        $tableGateway = $sm->get('Register\Model\RegisterTable');
        $tableGateway->myValidationMethod($value);

    }

}

Module.php

Module.php

public function getServiceConfig()
{
    return array(
        'factories' => array(
            'Register\Model\RegisterTable' =>  function($sm) {
                $tableGateway = $sm->get('RegisterTableGateway');
                $table = new RegisterTable($tableGateway);
                return $table;
            },
            'RegisterTableGateway' => function ($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new RegisterUser());
                return new TableGateway('table-name', $dbAdapter, null, $resultSetPrototype);
            },
        ),
    );
}

但是我得到致命错误:在非对象上调用成员函数get()
在模型类中获取ServiceLocator的正确方法是什么?

But I get Fatal error: Call to a member function get() on a non-object
What is a proper way to get ServiceLocator in model class?

推荐答案

您应将验证器的依赖项注入到验证器中.将验证器分配给表单字段时,可以通过options数组来实现. 我写了一些示例代码来说明我的意思:

You should inject the dependencies of the validator into the validator. You could do that through the options array when you assign the validator to a form field. I wrote up some sample code to demonstrate what I mean:

Register \ Validator \ MyValidation:

Register\Validator\MyValidation:

<?php
namespace Application\Validator;

use Zend\Validator\AbstractValidator;

class MyValidation extends AbstractValidator
{
    protected $tableGateway;

    public function __construct($options = null)
    {
        parent::__constructor($options);
        if ($options && is_array($options) && array_key_exists('tableGateway', $options))
        {
            $this->tableGateway = $options['tableGateway'];
        }           
    }

    public function isValid($value)
    {
        // ...
    }
}

对于表单,您可以实现ServiceLocatorAwareInterface,以便它随服务定位器自动注入,也可以使用表单的工厂将特定的依赖项注入表单.

As for the form you can either implement the ServiceLocatorAwareInterface, so it gets automatically injected with the service locator, or else inject specific dependencies into the form using a factory for the form.

使用ServiceLocatorAwareInterface的方法如下:

Here's how you would do it using the ServiceLocatorAwareInterface:

Register \ Form \ MyForm:

Register\Form\MyForm:

<?php
namespace Register\Form;

use Zend\Form\Form;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;

class MyForm extends Form implements InputFilterProviderInterface, ServiceLocatorAwareInterface
{
    protected $servicelocator;

    public function __construct()
    {
        $this->add(array(
                'name' => 'myfield',
                'attributes' => array(
                        'type' => 'text',
                ),
                'options' => array(
                        'label' => 'Field 1'
                ),
            )
        );  
    }

    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
    {
        $this->servicelocator = $serviceLocator;
    }

    public function getServiceLocator()
    {
        return $this->servicelocator;
    }

    public function getInputFilterSpecification()
    {
        return array(
            'myfield' => array(
                'required'    => true,
                'filters'     => array(),
                'validators'  => array(
                        array(
                            'name'    => 'Application\Validator\MyValidator',
                            'options' => array(
                                'tableGateway'    => $this->getServiceLocator()->get('Application\Model\RegisterTable'),
                            ),
                        ),
                ),
            ),
        );
    }
}

我也不清楚为什么要在验证器类中实例化一个Controller.你真的不应该那样做.

Also it's not clear to me why you are instantiating a Controller in your validator class. You really shouldn't do that.

这篇关于Zend Framework 2:如何在验证类中获取ServiceLocator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆