Search code examples
symfonysymfony-formssymfony-validator

Embedded form skipping validation


I have a User entity, related to a CreditCards entity (one to many), I am trying to write a form for users who just registered and don't have a Credit Card registered yet. In said form users are supposed to fill in the information for their first Credit Card.

The code below works, when it comes to building the form, the cvv text field displays "123" as it's supposed to.

But when I go to submit the form, the credit card data is not validated, only the user data, which is the issue.

Below are a few relevant lines of the code, if you need more portions ask, I will try to provide what's missing.

<?php
// in the controller
$user->addCreditCard(new CreditCard());
// this actually shows up in the form.
$user->getCreditCards()[0]->setCvv(123);
// in the outer form type
$builder->add(
    'credit_card',
    CollectionType::class,
    [
        'type' => CreditCardType::class,
        'property_path' => 'creditCards',
        'entry_options' => [
            //'validation_groups' => ['credit_card'],
            'container' => $container,
            // this is just me being desperate
            'constraints' => [new Assert\Valid(), new Assert\NotBlank()],
        ],
    ]
)
// Inner form type 
// This is extra, just to test if the failure happens at entity level. But this check is ignored.
$builder->add('number', TextType::class, ['constraints' => [new Assert\NotBlank()]]);

Solution

  • The issue was in the validation groups, I spelled default with a lowercase d. Apparently they are very case sentitive.