Symfony 表单验证 name='' 的无效表单控件不可聚焦

shu*_*van 1 php forms symfony

我有字段类型实体,下拉选择和必需 true 但提交表单时控制台中出现错误

An invalid form control with name='inbound_invoice_row[costObject]' is not focusable.
new:1 An invalid form control with  name='inbound_invoice_row[accountingAccount]' is not focusable.
new:1 An invalid form control with name='inbound_invoice_row[user]' is not focusable.
Run Code Online (Sandbox Code Playgroud)

另一个字段验证良好,例如增值税或价格,但对于会计帐户用户成本对象在控制台中出现此错误为什么不理解

我的表格

    /**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('location', EntityType::class, [
            'class' => Location::class,
            'empty_value' => 'select_default_value',
            'query_builder' => self::getLocations(),
            'required' => false,
            'label' => 'locations',
            'translation_domain' => 'invoicing'
        ])
        ->add('costObject', EntityType::class, [
            'class' => CostObject::class,
            'empty_value' => 'select_default_value',
            'choices' => self::getCostObjectHierarchy(),
            'required' => true,
            'label' => 'cost_object',
            'translation_domain' => 'invoicing'
        ])
        ->add('accountingAccount', EntityType::class, [
            'class' => AccountingAccount::class,
            'empty_value' => 'select_default_value',
            'query_builder' => self::getAccountingAccount(),
            'required' => true,
            'label' => 'accounting_account',
            'translation_domain' => 'invoicing'
        ])
        ->add('user', EntityType::class, [
            'class' => User::class,
            'empty_value' => 'select_default_value',
            'choices' => self::getR(),
            'required' => true,
            'label' => 'employee',
            'translation_domain' => 'invoicing'
        ])
        ->add('description', TextType::class, [
            'label' => 'description',
            'required' => false,
            'translation_domain' => 'invoicing'
        ])
        ->add('vat', ChoiceType::class, [
            'choices' => $this->vatClasses,
            'required' => true,
            'label' => 'vat',
            'translation_domain' => 'common'
        ])
        ->add('price', TextType::class, [
            'label' => 'price',
            'required' => true,
            'translation_domain' => 'invoicing'
        ]);
}
/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'EconomyBundle\Entity\InboundInvoiceRow',
        'locations' => [],
        'employees' => [],
        'accounts' => [],
        'vat' => [],
        'cost' => [],
        'ajax' => true,
        'csrf_protection' => true
    ));
}
public function getName()
{
    return 'inbound_invoice_row';
}
Run Code Online (Sandbox Code Playgroud)

在行动中创建形式

        $form = $this->createForm(
        $this->get('economy.form.type.in_bound_invoice_row'),
        $inboundInvoiceRow,
        [
            'validation_groups' => [InboundInvoiceRow::GROUP_POST],
            'cascade_validation' => true,
            'action' => $this->generateUrl('inbound_invoices_row_create', ['id' => $inboundInvoice->getId()]),
            'method' => 'POST',
        ]
    );

    $form->add('submit', 'submit', array('label' => 'save', 'translation_domain' => 'invoicing'));
Run Code Online (Sandbox Code Playgroud)

小智 5

您可能有一些在渲染这些字段时使用的 js 库(例如 Select2 或 Chosen)。当某个字段出现一些 HTML 验证错误(例如,该字段是必填字段,但没有值),但它不可见时(它的display属性可能设置为none),则浏览器无法将错误消息附加到该字段。这最有可能引发您的错误。

最简单的解决方案是设置'required' => false表单类型选项并依赖后端验证(例如使用 Symfony Validation 组件)而不是基本的 HTML 验证。