Symfony2 - ChoiceType - get choice list from JSON without JS

1.3k views Asked by At

I have a JSON file with all world languages and would like to put them into choices array inside ChoiceType form field.

$builder->add('languages', ChoiceType::class, array(
    'choices' => array()
))

My JSON file is stored: projectname/web/bundles/index/json/languages.json

Is it possible to achieve it without writing JS / AJAX?

P.S. EventListeners or other alternatives that Symfony2 provides suits me well.

1

There are 1 answers

0
Rawburner On BEST ANSWER

You could reach the file with DIR, since I dont' know where the php file with builder is located, it could looks like:

 $builder->add('languages', ChoiceType::class, array(
        'choices' => json_decode(
            //if builder is in controller, this should work
            file_get_contents(__DIR__.'/../../../web/bundles/index/json/languages.json'),
            true)
    ));