2

I would like to know if there is any way to put null value by default in the easyadmin_autocomplete select.

The first item in the list is selected and filled with data from database but i need a null value by default and setted automaticly. The goal is a first result point to an empty one (Choose one...).

Do you know how to do it?

Can you think of any way combining the options defined below?

#easy_admin.yml
Product:
      class: App\Entity\Product
      controller: App\Controller\ProductController
      new:
        fields:
          - { property: 'category', label: 'Category', type: 'easyadmin_autocomplete', type_options: { class: 'App\Entity\Category' } }
 }

In this example, one select has a placeholder with the text 'Any' (Ninguno). I need to know why is handling them different. form_pic These are the options defined for the attr fields -> type_options of the yml:

  1. action
  2. allow_extra_fields
  3. allow_file_upload
  4. attr
  5. auto_initialize
  6. block_name
  7. by_reference
  8. class
  9. compound
  10. constraints
  11. csrf_field_name
  12. csrf_message
  13. csrf_protection
  14. csrf_token_id
  15. csrf_token_manager
  16. data
  17. data_class
  18. disabled
  19. empty_data
  20. error_bubbling
  21. error_mapping
  22. extra_fields_message
  23. help
  24. help_attr
  25. inherit_data
  26. invalid_message
  27. invalid_message_parameters
  28. label
  29. label_attr
  30. label_format
  31. mapped
  32. method
  33. multiple
  34. post_max_size_message
  35. property_path
  36. required
  37. translation_domain
  38. trim
  39. upload_max_size_message
  40. validation_groups
Oscar
  • 1,929
  • 3
  • 16
  • 31

3 Answers3

2

If setting your default value would be a solution for you (like zero), service listener might be an answer:

// You need to add this listener yourself:
class ProductServiceListener
{
    ...

    // you can manipulate entity in this level as you wish:
    public function preUpdate(LifeCycleEventArgs $args)
    {

       // You will focus to Product entity, so block others:
       if (!(get_class($entity) == 'App\Entity\Product')) {
           return;
        }

        // Set whatever default value you want if it's null:
        if($entity->getCategory() == null) {
           $entity->setCategory(0); // Zero, as an example.
        }
tolga
  • 2,462
  • 4
  • 31
  • 57
1

There is no way to set a placeholder like value for easyadmin_autocomplete. The normal way for a choice type is to use the placeholder option as described in the symfony documentation. But easyadmin_autocomplete does not extend that type and it is a standalone type. Can't you use a choice type or entity type with the placeholder option. The only need for the easyadmin_autocomplete type is if there are a lot of entities and it will slow the application if all are loaded on the page.

My best bet is that you can extend the easyadmin_autocomplete. Because the easyadmin_autocomplete uses EntityType you can add the option for placeholder in the configureOptions method in the extended type. That will delegate the option to the EntityType but even then it is not displayed in the html because of the select2 javascript which also needs to be modified.

If it is a must I recommend the way to extend the type, add the option and also add custom javascript to easyadmin that will handle the new type and add the placeholder option. You can see the select2 documentation on how to set the placeholder.

But if you can use the entity or choice type for your select it is the preferred solution.

Simeon Kolev
  • 606
  • 1
  • 4
  • 17
1

Few years later in EasyAdmin 4 this problem still exists. If you like there is my custom JS fix which works globally

Your dashboard

class DashboardController extends AbstractDashboardController
{
    public function configureAssets(): Assets
    {
        return parent::configureAssets()
            ->addJsFile('custom.js');
    }
}

public/custom.js

document.addEventListener("DOMContentLoaded", function(){
    let selectFields = document.querySelectorAll('.form-select');
    if (selectFields.length > 0) {
        for (let i = 0; i < selectFields.length; i++) {

            // if TomSelect exists and select field does not have truly selected option while element.selectedIndex not working here
            if (selectFields[i].tomselect && selectFields[i].querySelector('option[selected]') === null) {
                selectFields[i].tomselect.clear();
            }
        }
    }
});
psionic
  • 11
  • 1