问题
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