Buy

EasyAdminBundle re-uses all the form stuff... but also comes with one new form field... and it's pretty bananas! From the main documentation page, click Edit and New Views Configuration. Down a ways, find a section called "Autocomplete". Ah, lovely! This is a lot like the EntityType... except that it renders as a fancy AJAX auto-complete box instead of a select drop down.

Right now, the subFamily field is a standard EntityType. But, it doesn't look that way at first... it's fancy! And has a search! We get this automatically thanks to some JavaScript added by EasyAdminBundle. It works wonderfully... as long as your drop down list is short. Because if there were hundreds or thousands of sub families... then all of them would need to be rendered on page load... which will really slow - or even break - your page.

Let's use the autocomplete field instead. Expand the subFamily configuration and set type: easyadmin_autocomplete:

171 lines app/config/config.yml
... lines 1 - 80
easy_admin:
... lines 82 - 91
entities:
Genus:
... lines 94 - 114
form:
fields:
... lines 117 - 124
- { property: 'subFamily', type: 'easyadmin_autocomplete' }
... lines 126 - 171

That is all we need: it will look at the subFamily field and know which entity to query. So.... it just works! Watch the web debug toolbar as a I type. Ha! There be AJAX happening!

Next, let's add a CollectionType to our form.

Leave a comment!

  • 2018-04-02 Victor Bocharsky

    Hey Dennis,

    Hm, if exactly this line causes that error - how is this createdBy field mapped in your code? I see we don't have such field :) I assume that createdBy is a User object, and you need to have a __toString() method in the User class, not in Genus. Otherwise, specify the "class" option explicitly.

    Cheers!

  • 2018-03-30 Dennis

    Hi Ryan, I get an error : "The required option "class" is missing.". I've googled this problem, and it happens when you do not have a __toString() method in your entity, but I do have that.

    Do you have any clue why this may happen?

    My code in config.yml: "- { property: 'createdBy', type: 'easyadmin_autocomplete' }"

    Best regards,
    Dennis