Symfony表单验证名称为'''的无效表单控件不可聚焦

时间:2017-02-23 17:04:50

标签: php forms symfony

我有for字段类型实体,下拉选项和必需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.

另一个字段验证很好,如增值税或价格,但对于accountingAccount用户costObject在控制台中有此错误 为什么不明白

我的表格

    /**
 * @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';
}

创建表单

        $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'));

1 个答案:

答案 0 :(得分:2)

您可能在渲染这些字段时使用了一些js库(例如Select2或Chosen)。如果字段上有一些HTML验证错误(例如字段是必需的但没有值),但它不可见 - 它可能将display属性设置为none - 那么浏览器无法将错误消息附加到该字段。这很可能会触发您的错误。

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