Use a static function to define an array for the choices options of a form

I use the easyAdminBundle, I would like to know if it’s possible to use a static function or const (define anywhere in my app) to set the choices option of a choice type as such :

- { property: tag, type: choice, type_options: { choices: 'AppEntityNews::getTags' }

With a getTags function like :

class News 
    const TAGS = ['toto','tutu'];

    static public function getTags()
        return $this::TAGS;

It is already possible to do that with query_builder but I didn’t find any trace of it in the documentation.

Actually I get the following error which lead me to think it’s not possible (but maybe someone here do):

An error has occurred resolving the options of the form “SymfonyComponentFormExtensionCoreTypeChoiceType”: The option “choices” with value “AppEntityNews::getTags” is expected to be of type “null” or “array” or “Traversable”, but is of type “string”.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *