Easyadminbundle: Howto set custom data-provider for choice field?

Created on 14 Jan 2016  路  3Comments  路  Source: EasyCorp/EasyAdminBundle

Hey, I'm real in trouble because I have a project which have to go live in next days. How can I tell EasyAdminBundle a custom data-provider for a "choice" field?

I have a Method which returns an array, this array should be shown in form as choice field.

Most helpful comment

@amit0rana you are right ... but @SebTM has a method that generates the list of options dynamically. In this case, you must create your own AdminController (which is very easy to do) and then override the method that creates the form for your entity:

class AdminController extends BaseAdminController
{
    public function create<EntityName>EntityFormBuilder($entity, $view)
    {
        $formBuilder = parent::createEntityFormBuilder($entity, $view);

        // get the list of choices from your application and add the form field for them
        $formBuilder->add(...);

        return $formBuilder;
    }
}

All 3 comments

I am new to easyadmin myself so this might not be the best solution. If hardcoding of choices is an option you can do following:

form:
    fields:
        - { property: 'gender', type:'choice', type_options: {choices: {'1':'Male', '2':'Female'}}}

This will show a choice with Male/Female option.

@amit0rana you are right ... but @SebTM has a method that generates the list of options dynamically. In this case, you must create your own AdminController (which is very easy to do) and then override the method that creates the form for your entity:

class AdminController extends BaseAdminController
{
    public function create<EntityName>EntityFormBuilder($entity, $view)
    {
        $formBuilder = parent::createEntityFormBuilder($entity, $view);

        // get the list of choices from your application and add the form field for them
        $formBuilder->add(...);

        return $formBuilder;
    }
}

@amit0rana Thanks for your help but in this case I really need a dynamic solution as the drop-down offers some paths which depends on FTP-Uploads

@javiereguiluz Thanks for your help!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

haithem-rihane picture haithem-rihane  路  4Comments

seb-jean picture seb-jean  路  3Comments

ndench picture ndench  路  4Comments

nickicool picture nickicool  路  4Comments

BigMichi1 picture BigMichi1  路  3Comments