【问题标题】:Arguments not being supplied to service未提供服务的参数
【发布时间】:2013-04-29 07:09:46
【问题描述】:

我的验证器服务未提供给验证器。我收到一个错误: “警告:My\Bundle\Validator\Constraints\MyCustomValidator::__construct() 缺少参数 1...”

这是我的 services.yml

// My\Bundle\Resources\config\services.yml
services:
    my.validator.service:
        class: My\Bundle\Validator\Constraints\MyCustomValidator
        arguments: [ @doctrine.orm.entity_manager ]

这是我的验证器类:

// My\Bundle\Validator\Constraints\MyCustomValidator
namespace My\Bundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Doctrine\ORM\EntityManager;

class MyCustomValidator extends ConstraintValidator
{
    private $em;

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

    public function validate($value, Constraint $constraint)
    {
        // Do something
    }
}

这是我的validation.yml

// My\Bundle\Resources\config\validation.yml
My\Bundle\Entity\Page:
    properties:
        name:
            - NotBlank: ~
            - My\Bundle\Validator\Constraints\MyCustom: ~

这是我的约束类

namespace My\Bundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

class MyCustom extends Constraint
{
    public $message = 'Something is wrong with "%string%".';

    public function validatedBy()
    {
        return get_class($this) . 'Validator';
    }
}

如果有人可以帮助我,我将非常感激。

我也尝试将参数名称更改为“@doctrine.orm.default_entity_manager”,但没有成功。

【问题讨论】:

  • 你是如何使用它的?
  • 说实话,我还没有真正做到这一点。我想在 validate 函数中执行查询并根据查询结果添加错误。但我什至无法通过最简单的部分。
  • 配置正确。错误是否发生在容器类中?尝试清除缓存?
  • 检查原则唯一实体约束及其构建方式(并在原则捆绑中定义),您应该直接使用管理器注册表而不是实体管理器。
  • 您有注释类,或者您如何将验证器应用于实体?您在什么情况下收到此错误?

标签: symfony


【解决方案1】:

问题是我的 services.yml。因为该服务被用作验证器,所以我必须使用 validator.constraint_validator 标记。这是在文档中。哎呀!

services:
    my.validator.service:
        class: My\Bundle\Validator\Constraints\MyCustomValidator
        arguments: [ @doctrine.orm.entity_manager ]
        tags:
            - { name: validator.constraint_validator, alias: my_custom_alias}

我还需要重写 Constraint 类的validateBy() 方法,以便它返回上面的别名,例如:

// My\Bundle\Validator\Constrains\MyCustom.php
public function validatedBy()
{
    return 'my_custom_alias';
}

【讨论】:

    【解决方案2】:

    您应该将原则注册表传递给您的服务并使用它来检索实体管理器实例。

    将您的构造函数更改为:

    public function __construct(RegistryInterface $registry)
    {
        $this->registry = $registry;
    }
    

    然后将您的服务配置修改为:

    // My\Bundle\Resources\config\services.yml
    services:
        my.validator.service:
            class: My\Bundle\Validator\Constraints\MyCustomValidator
            arguments: [ @doctrine ]
    

    现在任何时候您需要在MyCustomValidator 中获得实体经理,您都可以这样做

    $em = $this->registry->getManager()
    

    【讨论】:

    • 感谢您的回答。我仍然收到相同的错误,表明我的任何参数都没有传递给构造函数:“可捕获的致命错误:传递给 My\Bundle\Validator\Constraints\MyCustomValidator::__construct() 的参数 1 必须是“.. .",没有给出"。我在这里遗漏了一些东西。我的验证器已正确注册为服务,只是由于某种原因没有收到参数?我不知道有任何其他设置可能导致这种行为。
    猜你喜欢
    • 2015-10-15
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    相关资源
    最近更新 更多