Symfony Custom Validator未加载依赖项

时间:2015-03-11 21:28:40

标签: php symfony

错误:

...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

EDIT2

在表单服务

中使用时可以找到它
dashboardhub_app_main.form.type.dashboard:
  class: DashboardHub\Bundle\AppBundle\Form\DashboardType
  arguments: ["%dashboard_hub_app%"]
  tags:
    - { name: form.type, alias: dashboard }

1 个答案:

答案 0 :(得分:0)

parameters:
  dashboard_hub_app: -- !!!!! IS NULL yes
有趣的:) 参数一个接一个,所以你需要小心,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)