gpt4 book ai didi

symfony - 在实体之外使用 UniqueEntity 并且不使用表单

转载 作者:行者123 更新时间:2023-12-01 11:20:34 26 4
gpt4 key购买 nike

我需要验证用户传递的电子邮件:

private function validate($value): bool
{
$violations = $this->validator->validate($value, [
new Assert\NotBlank(),
new Assert\Email(),
new UniqueEntity([
'entityClass' => User::class,
'fields' => 'email',
])
]);

return count($violations) === 0;
}

但是 UniqueEntity 约束抛出异常:

Warning: get_class() expects parameter 1 to be object, string given



看起来像 ValidatorInterface::validate() 方法的第一个参数正在等待带有 getEmail() 方法的实体对象,但它看起来很难看。

是否有任何优雅的方法来验证仅将标量值传递给 ValidatorInterface::validate() 方法的字段的唯一性?

最佳答案

似乎没有内置的 Symfony 解决方案来做我想做的事,所以我按照 Jakub Matczak 的建议创建了自定义约束。

UPD: 当您发送表单以编辑您的实体时,此解决方案会引发验证错误。为避免此行为,您需要手动改进此约束。

约束:

namespace AppBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

class UniqueValueInEntity extends Constraint
{
public $message = 'This value is already used.';
public $entityClass;
public $field;

public function getRequiredOptions()
{
return ['entityClass', 'field'];
}

public function getTargets()
{
return self::PROPERTY_CONSTRAINT;
}

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

验证器:
namespace AppBundle\Validator\Constraints;

use Doctrine\ORM\EntityManager;
use InvalidArgumentException;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class UniqueValueInEntityValidator extends ConstraintValidator
{
/**
* @var EntityManager
*/
private $em;

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

public function validate($value, Constraint $constraint)
{
$entityRepository = $this->em->getRepository($constraint->entityClass);

if (!is_scalar($constraint->field)) {
throw new InvalidArgumentException('"field" parameter should be any scalar type');
}

$searchResults = $entityRepository->findBy([
$constraint->field => $value
]);

if (count($searchResults) > 0) {
$this->context->buildViolation($constraint->message)
->addViolation();
}
}
}

服务:
services:
app.validator.unique_value_in_entity:
class: AppBundle\Validator\Constraints\UniqueValueInEntityValidator
arguments: ['@doctrine.orm.entity_manager']
tags:
- { name: validator.constraint_validator }

用法示例:
private function validate($value): bool
{
$violations = $this->validator->validate($value, [
new Assert\NotBlank(),
new Assert\Email(),
new UniqueValueInEntity([
'entityClass' => User::class,
'field' => 'email',
])
]);

return count($violations) === 0;
}

关于symfony - 在实体之外使用 UniqueEntity 并且不使用表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44199711/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com