ZF3 dbadapters 没有进入表单验证

ZF3 dbadapters not getting in form validation

我已经按照以下配置了 dbadapters。

在 config/autoload/global 中添加了以下代码。php

  use Zend\Session\Storage\SessionArrayStorage;
  use Zend\Session\Validator\RemoteAddr;
  use Zend\Session\Validator\HttpUserAgent;

  use Zend\Db\Adapter;
  use Zend\ServiceManager\Factory\InvokableFactory;


   return [
// Session configuration.
'session_config' => [
    'cookie_lifetime' => 60 * 60 * 1, // Session cookie will expire in 1 hour.
    'gc_maxlifetime' => 60 * 60 * 24 * 30, // How long to store session data on server (for 1 month).        
],
// Session manager configuration.
'session_manager' => [
    // Session validators (used for security).
    'validators' => [
        RemoteAddr::class,
        HttpUserAgent::class,
    ]
],
// Session storage configuration.
'session_storage' => [
    'type' => SessionArrayStorage::class
],

'service_manager' => [
    'abstract_factories' => [
        Adapter\AdapterAbstractServiceFactory::class,
    ],
    'factories' => [
        Adapter\AdapterInterface::class => Adapter\AdapterServiceFactory::class,
    ],
    'aliases' => [
        Adapter\Adapter::class => Adapter\AdapterInterface::class,
    ],  
],

];

之后,我将以下代码添加到 config/autoload/development。local.php

return [
'view_manager' => [
    'display_exceptions' => true,
],

'doctrine' => [
    'connection' => [
        'orm_default' => [
            'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
            'params' => [
                'host' => 'host',
                'user' => 'root',
                'password' => 'root',
                'dbname' => 'dbname',
            ]
        ],
    ],
],

'db' => [
    'adapters' => [
        'db' => [
            'driver' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
            'dsn' => 'mysql:dbname=dbname;host=host',
            'driver_options' => [
                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
            ],
            'username' => 'root',
            'password' => 'root',
        ],
    ],
],
];

如何在控制器和表单中获取dbAdapters?

我想在表单过滤器中使用 NoRecordExists 验证。 喜欢

  array(
                    'name' => 'Zend\Validator\Db\NoRecordExists',
                    'options' => array(
                        'table' => 'test_user',
                        'field' => 'email',
                        'adapter' => $this->dbAdapter,
                        'exclude' => $this->excludeFields,
                        'messages' => array(
                            \Zend\Validator\Db\NoRecordExists::ERROR_RECORD_FOUND => 'Email already exists'
                        ),
                    ),
                    'break_chain_on_failure' => true
                ) 

分享你的想法或给我建议在 ZF3 中全局配置 dbAdapters。

您需要使用工厂将适配器注入到表单中。我不确定开箱即用是否支持使用 Doctrine 进行表单验证,因此我的示例将使用常规的旧 zend-db。

简而言之:

设置一个简单的工厂并使用依赖注入将适配器提供给表单。

形式

表单需要接受依赖,所以给构造函数添加一个参数:

/**
 * @var \Zend\Db\Adapter\Adapter
 */
private $dbAdapter;

/**
 * @param \Zend\Db\Adapter\Adapter $adapter
 */
public function __construct(\Zend\Db\Adapter\Adapter $adapter)
{
    $this->dbAdapter = $adapter;
}

工厂

namespace MyModule\Factory\Form;

use Zend\ServiceManager\Factory\FactoryInterface;

/**
 * Class MyFormFactory
 * @package MyModule\Factory\Form
 */
class MyFormFactory implements FactoryInterface
{
    /**
     * Create an object
     *
     * @param  ContainerInterface $container
     * @param  string             $requestedName
     * @param  null|array         $options
     *
     * @return object
     * @throws ServiceNotFoundException if unable to resolve the service.
     * @throws ServiceNotCreatedException if an exception is raised when
     *     creating a service.
     * @throws ContainerException if any other error occurs
     */
    public function __invoke(
        ContainerInterface $container,
        $requestedName,
        array $options = null
    ) {
        /** @var \Zend\Db\Adapter\AdapterInterface $adapter */
        $adapter  = $container->get(AdapterInterface::class);

        return new MyForm($adapter);
    }
}

最后:

在您的 module.config.php 中,您需要告诉您的应用程序调用工厂来设置表单:

'service_manager_' => [
    'factories' => [
        Form\MyForm::class => Factory\Form\MyFormFactory::class
    ]
],

作为Dymen1 mentioned you could probably benefit from trying out the tutorial in the documentation解释这个主题的地方。