Symfony2 - How to validate autocomplete entity form type?

早过忘川 提交于 2019-12-23 12:26:14

问题


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,
    ]);

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!


回答1:


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.

Data transformer example

Validation will work as if it was entity field type, thanks to the data transformer.



来源:https://stackoverflow.com/questions/31938070/symfony2-how-to-validate-autocomplete-entity-form-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!