Search code examples
symfonyphp-8customvalidator

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


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();

Solution

  • 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                
        ])
     }