Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony不';t加载定义为服务的自定义验证器_Php_Symfony - Fatal编程技术网

Php Symfony不';t加载定义为服务的自定义验证器

Php Symfony不';t加载定义为服务的自定义验证器,php,symfony,Php,Symfony,我正在尝试将EntityManagerInterface注入我的自定义验证器,但出现以下错误: Attempted to load class "validator_check_client_id" from the global namespace. Did you forget a "use" statement? 我的代码是: CheckClientId.php namespace-App\Validator; 使用Symfony\Compone

我正在尝试将EntityManagerInterface注入我的自定义验证器,但出现以下错误:

Attempted to load class "validator_check_client_id" from the global namespace.
Did you forget a "use" statement?

我的代码是:

CheckClientId.php

namespace-App\Validator;
使用Symfony\Component\Validator\Constraint;
/**
*@注释
*/
类CheckClientId扩展约束{
public$message='Test';
公共函数validatedBy(){
返回“验证器检查客户id”;
}
}
checkclientdvalidator.php

namespace-App\Validator;
使用条令\ORM\EntityManagerInterface;
使用Symfony\Component\Validator\Constraint;
使用Symfony\Component\Validator\ConstraintValidator;
类checkClientValidator扩展了ConstraintValidator{
私人$entityManager;
公共函数构造(entityManager接口$entityManager){
$this->entityManager=$entityManager;
}
公共函数验证($value,Constraint$Constraint){
//待办事项
返回;
}
}
服务。yaml

validator.check\u客户端\u id:
类:App\Validator\checkclientdvalidator
自动连线:错误
论据:
$entityManager:“@doctrine.orm.entity_manager”
标签:
- {
名称:validator.constraint\u validator,
别名:验证程序\u检查\u客户端\u id,
}
我应该多做一步来注册我的服务吗?我认为symfony找不到它,因为它没有正确注册

我也试着按照这个答案去做,但我得到了一个类似的错误:

Attempted to load class "app.validator.blog.slug_unique" from the global namespace.
Did you forget a "use" statement?
你知道发生了什么事吗


谢谢你的帮助

多亏了autowire和autoconfig,您真正需要做的就是正确地命名类名。对services.yaml没有任何更改。下面是一个使用新的5.2项目的工作示例

# src/Validator/CheckClientId.php
namespace App\Validator;

use Symfony\Component\Validator\Constraint;

/** @Annotation */
class CheckClientId extends Constraint
{
    public $message = 'CheckClientId';
}
# =============================
# src/Validator/CheckClientIdValidator
namespace App\Validator;

use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

class CheckClientIdValidator extends ConstraintValidator
{
    public function __construct(private EntityManagerInterface $entityManager)
    {

    }
    public function validate($value, Constraint $constraint)
    {
        dump('validate ' . $value);
        throw new UnexpectedValueException($value, 'string');
    }
}
# ===============================
# Test Entity
# src/Entity/Client.php
namespace App\Entity;

use App\Validator as AcmeAssert;

class Client
{
    /** @AcmeAssert\CheckClientId */
    public $id = 42;
}
# ====================================
# Test Command
# src/Command/ValidateCommand
namespace App\Command;

use App\Entity\Client;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;

class ValidateCommand extends Command
{
    protected static $defaultName = 'test:validator';

    public function __construct(private ValidatorInterface $validator)
    {
        parent::__construct();
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $client = new Client();
        $errors = $this->validator->validate($client);
        echo $errors;

        return Command::SUCCESS;
    }
}

让test命令正常工作,然后从那里继续。

尝试使用
返回“validator.check_client_id”(注意点而不是第一个下划线)?您使用的是哪个版本的Symfony?Constraint::validatedBy需要返回验证器类的完全限定名。类似于:。更好的是,只需遵循Symfony命名约定,让默认代码工作即可。然后您的验证程序服务需要使用与服务id相同的类名。如果您只是从services.yaml中删除配置并让autowire完成它的工作,那么它应该可以正常工作。如果我使用return“validator.check_client_id”;我有一个错误:试图从全局命名空间加载类“validator.check\u client\u id”。您是否忘记了“use”语句?在上面的链接中:如果您使用的是default services.yaml配置,那么您的验证器已经注册为服务,并使用必要的validator.constraint\u验证器进行标记。这意味着您可以像任何其他服务一样注入服务或配置。。。换句话说,您不需要将其注册为服务。尝试使用validator.check\u client\u id是导致您出现问题的原因。非常感谢!我在我的项目中复制了您的代码,虽然它在控制台中工作,但在浏览器中尝试时对我不起作用,因此我更改了验证方式,再次非常感谢!)