gpt4 book ai didi

Symfony - CollectionType 字段中的唯一实体?

转载 作者:行者123 更新时间:2023-12-03 08:49:52 26 4
gpt4 key购买 nike

我有一个包含 collectionType 的 formBuilder。我希望能够对电子邮件字段施加限制,以确保当用户验证时,表单中不会多次出现相同的电子邮件地址

我已经:

RegistrationCollectionType.php

$builder
->add('utilisateurs', CollectionType::class, [
'entry_type' => RegistrationType::class,
'entry_options' => [
'label' => false,
'entreprise' => $entreprise,
],
'allow_add' => true,
'allow_delete' => true,
'delete_empty' => true,
'by_reference' => true,
'prototype' => true,
'label' => false,
'attr' => [
'class' => 'my-selector',
'label' => false,
],
'by_reference' => false,
])
;

与他的类(class):

RegistrationCollection.php

class RegistrationCollection
{


private $utilisateurs = [];

public function getUtilisateurs(): ?array
{
return $this->utilisateurs;
}

public function setUtilisateurs(?array $utilisateurs): self
{
$this->utilisateurs = $utilisateurs;

return $this;
}
}

在与我的 User 实体关联的 RegistrationType.php 中,我:

RegistrationType.php

->add('email', EmailType::class, [
'attr' => [
'placeholder' => "Adresse email"
],
])

enter image description here

现在,如果我有效,我已经:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicata du champ '[email protected]' pour la clef 'UNIQ_8D93D649E7927C74'

最佳答案

我保留了自定义约束的想法,但这不仅适用于电子邮件,还适用于我们想要唯一的任何字段:

#App\Validator\Constraints\UniqueProperty.php

<?php

namespace App\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
*/
class UniqueProperty extends Constraint
{
public $message = 'This collection should contain only elements with uniqe value.';
public $propertyPath;

public function validatedBy()
{
return UniquePropertyValidator::class;
}
}

#App\Validator\Constraints\UniquePropertyValidator.php

<?php

namespace App\Validator\Constraints;

use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

class UniquePropertyValidator extends ConstraintValidator
{
/**
* @var \Symfony\Component\PropertyAccess\PropertyAccessor
*/
private $propertyAccessor;

public function __construct()
{
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
}

/**
* @param mixed $value
* @param Constraint $constraint
* @throws \Exception
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof UniqueProperty) {
throw new UnexpectedTypeException($constraint, UniqueProperty::class);
}

if (null === $value) {
return;
}

if (!\is_array($value) && !$value instanceof \IteratorAggregate) {
throw new UnexpectedValueException($value, 'array|IteratorAggregate');
}

if ($constraint->propertyPath === null) {
throw new \Exception('Option propertyPath can not be null');
}

$propertyValues = [];
foreach ($value as $key => $element) {
$propertyValue = $this->propertyAccessor->getValue($element, $constraint->propertyPath);
if (in_array($propertyValue, $propertyValues, true)) {

$message = sprintf("%s (%s)", $constraint->message, $propertyValue);

$this->context->buildViolation($message)
// ->atPath(sprintf('[%s]', $key))
->atPath(sprintf('[%s][%s]', $key, $constraint->propertyPath))
->addViolation();
}

$propertyValues[] = $propertyValue;
}
}
}

class RegistrationCollection
{


/**
* @App\UniqueProperty(
* message = "Adresse mail déjà utilisée",
* propertyPath = "email"
* )
*
*/
private $utilisateurs = [];

它工作得很好,只是我无法将错误定位到子字段。系统地,错误将转到父实体,因此错误将被全部覆盖。

我尝试在验证器中重定向到相关子实体的字段,但无济于事,错误继续将所有内容放在上面..

enter image description here

enter image description here

在我的 FormType 中,我尝试禁用 error_bubbling 但同样的事情

->add('utilisateurs', CollectionType::class, [
'entry_type' => RegistrationType::class,
'entry_options' => [
'label' => false,
'entreprise' => $entreprise,
],
'allow_add' => true,
'allow_delete' => true,
'delete_empty' => true,
'by_reference' => true,
'prototype' => true,
'label' => false,
'attr' => [
'class' => 'my-selector',
'label' => false,
],
'by_reference' => false,
'error_bubbling' => false,
])
;

关于Symfony - CollectionType 字段中的唯一实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59705795/

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