错误不是绑定在CollectionType字段上,而是在根FormType上。

huangapple go评论55阅读模式
英文:

Error is not bind on the CollectionType field but on the root FormType

问题

我尝试创建一个简单的表单,其中包含几种不同的FormType以及在FormType类中使用验证器(但如果我在实体上编写验证器,同样的问题也会出现)。

所以我有一个实体叫做“Phase”:

class Phase(省略了Doctrine注释和getter/setter)
{
    private int $idPhase;
    private ?DateTimeInterface $dtDue = null;
    private int $nbNoticeDaysExpert = 7;
    private int $nbNoticeDaysMember = 7;
}

我有一个包含这个第一个实体“Phase”集合的“父”实体“Call”:

class Call(省略了Doctrine注释和getter/setter)
{
    private ?int $idCall = null;
    private ?string $acronym = null;
    private ?Call $previousCall = null;
    private Collection $phases;

    public function __construct()
    {
        $this->phases = new ArrayCollection();
    }
}

我创建了基本的约束和验证器类,并且在我的示例中,将其附加到我的FormType的每个属性上:

class PhaseUniqueByCallValidator extends ConstraintValidator
{
    public function validate(mixed $value, Constraint $constraint): void
    {
        $this->context->buildViolation('paffffff')->addViolation();
        return;
    }
}

我注意到,如果我使用Symfony验证器(例如Count),完全相同的情况会发生。

我像往常一样创建了表单类型:

class CallType extends AbstractType
{
    $builder
        ->add('lbAcronyme', TextType::class, [
            'required' => true,
            'constraint' => [new PhaseUniqueByCall()],
        ])
        ->add('previousCall', EntityType::class, [
            'class' => Call::class,
            'constraint' => [new PhaseUniqueByCall()],
        ])
        ->add('phases', CollectionType::class, [
            'entry_type' => Phase::class,
            'constraint' => [new PhaseUniqueByCall()],
        ])
}

所以这就是我的问题,CollectionType的错误不会呈现在HTML中的字段下,因为它没有附加到FormTypeCollectionType

当我在Twig中进行转储时,我可以看到根节点上的错误。

所以我可能忘了指定某些东西吗?我搜索了几个小时(也许是错的,哈哈),但我没有找到与我有相同问题的人,也没有在Symfony Git的问题中找到。

我甚至尝试阅读供应商代码以查找错误附加到FormType的位置,但代码太多了,我没有找到。

我尝试使用ConstraintViolationBuilder类的atPath函数来处理,但没有成功,我也没有找到明确的示例来使用它。

$this->context->buildViolation($constraint->messageFirst)
                ->atPath('phases')
                ->addViolation();
英文:

I tried to make a simple Form with several different FormType with Validators in the FormType class (but same problem appears if i write validators on entity).

So i have entity "Phase"

class Phase (omitting doctrine annotations ans getter/setter)
{
    private int $idPhase;
    private ?DateTimeInterface $dtDue = null;
    private int $nbNoticeDaysExpert = 7;
    private int $nbNoticeDaysMember = 7;
}

I have 'parent' entity "Call" containing a collection of this first entity "Phase"

class Call (omitting doctrine annotations ans getter/setter)
{
    private ?int $idCall = null;
    private ?string $acronym = null;
    private ?Call $previousCall = null;
    private Collection $phases;

    public function __construct()
    {
        $this->phases = new ArrayCollection();
    }
}

I created basic Constraint and Validator classes and for my example, il attached it to each Property of my FormType

class PhaseUniqueByCallValidator extends ConstraintValidator
{
    public function validate(mixed $value, Constraint $constraint): void
    {
        $this->context->buildViolation('paffffff')->addViolation();
        return;
    }
}

> I noticed that exactly the same happens if i use Symfony Validator (for exemple Count)

I have created the formType as usual.

class CallType extends AbstractType
{
    $builder
        ->add('lbAcronyme', TextType::class, [
            'required' => true,
            'constraint' => [new PhaseUniqueByCall()],
        ])
        ->add('previousCall', EntityType::class, [
            'class' => Call::class,
            'constraint' => [new PhaseUniqueByCall()],
        ])
        ->add('phases', CollectionType::class, [
            'entry_type' => Phase::class,
            'constraint' => [new PhaseUniqueByCall()],
        ])
}

And So there is my problem, the Errors for the CollectionType is not rendered under its field in html because not attached to the formtype CollectionType
html rendered

When i dump in twig, i can see the error on the root
dump of FormType in twig

So may i have forget to specify something ? i searched several hours (maybe bad lol) but i did not find anybody having the same problem than me and i did not find either on issues of Symfony Git.

I even try to read code in vendor to find where the error was attached ti the formType but so many code that i did not find.

I have tried to deal with the function atPath of the ConstraintViolationBuilder class but without success and i did not find clear examples how to use it.

$this->context->buildViolation($constraint->messageFirst)
                ->atPath('phases')
                ->addViolation();

答案1

得分: 0

你应该在集合表单中关闭错误冒泡。

class CallType extends AbstractType
{
    $builder
        ->add('lbAcronyme', TextType::class, [
            'required' => true,
            'constraint' => [new PhaseUniqueByCall()],
        ])
        ->add('previousCall', EntityType::class, [
            'class' => Call::class,
            'constraint' => [new PhaseUniqueByCall()],
        ])
        ->add('phases', CollectionType::class, [
            'entry_type' => Phase::class,
            'constraint' => [new PhaseUniqueByCall(), new Valid()],
            'error_bubbling' => false                
        ])
}
英文:

You should turn off error bubbling in collection form.

class CallType extends AbstractType
{
    $builder
    ->add('lbAcronyme', TextType::class, [
        'required' => true,
        'constraint' => [new PhaseUniqueByCall()],
    ])
    ->add('previousCall', EntityType::class, [
        'class' => Call::class,
        'constraint' => [new PhaseUniqueByCall()],
    ])
    ->add('phases', CollectionType::class, [
        'entry_type' => Phase::class,
        'constraint' => [new PhaseUniqueByCall(), new Valid()],
        'error_bubbling' => false                
    ])
 }

huangapple
  • 本文由 发表于 2023年7月11日 15:02:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76659415.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定