繁体   English   中英

Symfony自定义验证器未加载依赖项

[英]Symfony Custom Validator not loading Dependency

错误:

...ThemeValidator::__construct() must be of the type array, null given...

由于某种原因,未调用Service,但直接加载了Class。

validation.yml

theme:
        - NotBlank: ~
        - DashboardHub\Bundle\AppBundle\Validator\Constraints\ThemeValidator: ~

验证者类别

<?php
namespace DashboardHub\Bundle\AppBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class ThemeValidator extends ConstraintValidator
{

protected $config;

public function __construct(array $config)
{
    $this->config = $config;
}

public function validatedBy()
{
    return 'theme.validator';
}

public function validate($value, Constraint $constraint)
{
    var_dump($this->config); exit;
}
// ...

service.yml

dashboardhub_app_main.validator.constraints.theme:
   class: DashboardHub\Bundle\AppBundle\Validator\Constraints\ThemeValidator
   arguments: ["%dashboard_hub_app%"]
   tags:
     - { name: validator.constraint_validator, alias: theme.validator }

编辑

parameters:
  dashboard_hub_app:
    themes:
        Github: DashboardHubAppBundle:Template:Github.html.twig
        GithubTravis: DashboardHubAppBundle:Template:GithubTravis.html.twig

编辑2

在Form Service中使用时可以找到

dashboardhub_app_main.form.type.dashboard:
  class: DashboardHub\Bundle\AppBundle\Form\DashboardType
  arguments: ["%dashboard_hub_app%"]
  tags:
    - { name: form.type, alias: dashboard }
parameters:
  dashboard_hub_app: -- !!!!! IS NULL yes

funny :)参数一个接一个地跟随,所以您需要小心, paramenters && themes -是不同的。

注入一个数组,您需要为此参数提供一个值,但我想您需要注入一个

themes:
    Github: DashboardHubAppBundle:Template:Github.html.twig
    GithubTravis: DashboardHubAppBundle:Template:GithubTravis.html.twig

arguments: ["%themes%"]

结果将是Validator类中的一个数组

array (size=2)
  'Github' => string 'DashboardHubAppBundle:Template:Github.html.twig' (length=47)
  'GithubTravis' => string 'DashboardHubAppBundle:Template:GithubTravis.html.twig' (length=53)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM