我有一个用户实体,与信用卡实体(一对多)相关,我正在尝试为刚刚注册但尚未注册信用卡的用户编写表单。在上述表格中,用户应该填写他们第一张信用卡的信息。
下面的代码有效,在构建表单时,cvv 文本字段按预期显示“123”。
但是当我去提交表单时,信用卡数据没有被验证,只有用户数据,这是问题所在。
下面是一些相关的代码行,如果您需要更多部分,我会尽力提供缺少的内容。
<?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()]]);