Symfony2-如何验证自动完成实体表单类型? [英] Symfony2 - How to validate autocomplete entity form type?
问题描述
我有一个包含以下字段的表单:
I have a form with following fields:
$builder
->add('title', 'text')
->add('body', 'textarea')
->add('tags', 'entity', [
'class' => 'AppBundle\Entity\Tag',
'choice_label' => 'name',
'expanded' => false,
'multiple' => true,
]);
用户可以选择多个标签.一切正常.但是现在,当标签数量变得非常大(超过20000个标签)时,页面渲染变得非常慢,因为实体类型将所有标签加载到选择框中.因此,我实现了一个jQuery自动完成选择框,以防止加载所有实体,但是当我提交表单时,验证程序仍会加载所有标签以进行验证!如何解决此验证问题?谢谢!
User can select multiple tags. Everything works perfectly. But now when the numbers of tag become very large (over 20000 tags), page rendering become very slow because entity type loads all tag into selectbox. Therefore I implement a jQuery autocomplete selectbox to prevent loading all entity but when I submit the form, validator still loads all tags to validate! How can I solve this validation issue? Thank you!
推荐答案
代替使用entity
字段类型,而使用将接受关联实体ID的简单text
类型.您还需要使数据转换器将提交的ID转换为将在表单的数据实体上设置的实体对象(反之亦然).
Instead of using entity
field type, use simple text
type that will accept ID of the associated entity. You also need to make data transformer to transform submitted ID to entity object (and vice versa) that will be set on the form's data entity.
由于数据转换器的作用,验证将像entity
字段类型一样工作.
Validation will work as if it was entity
field type, thanks to the data transformer.
这篇关于Symfony2-如何验证自动完成实体表单类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!