首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony表单验证名称为‘’的无效表单控件不可聚焦

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

Stack Overflow用户
提问于 2017-02-24 01:04:50
回答 1查看 1.4K关注 0票数 0

我有for with字段类型entity,下拉选择和required true,但当提交表单在控制台中有错误时

代码语言:javascript
复制
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.

另一个字段validate fine,如vat或price,但对于accountingAccount用户,costObject在控制台中有此错误为什么不理解

我的表单

代码语言:javascript
复制
    /**
 * @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';
}

创建实际操作的表单

代码语言:javascript
复制
        $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'));
EN

回答 1

Stack Overflow用户

发布于 2017-02-24 01:53:28

您可能有一些在呈现这些字段时使用的js库(例如Select2或Select2)。当一个字段上有一些超文本标记语言验证错误(例如,字段是必需的,但没有值),但它是不可见的-它可能有display属性设置为none -那么浏览器无法将错误消息附加到该字段。这是最有可能触发错误的原因。

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42422006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档