Julie
Julie

Reputation: 583

How to fill a dropdown list from an excel file

I have a dropdown list that should be filled from an excel file column for now I am filling the list directly:

Formbuilder:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
->add('Author', ChoiceType::class, array(
                'choices'  => array(
                    'Author1' => 'Author1',
                    'Author2' => 'Author2',
                    'Author3' => 'Author3'

                )))
;
    }

twig

<div class="col-xs-9 col-sm-9 col-md-9 col-lg-9">
        {{ form_widget(form.Author, {'attr': {'class' : 'form-control '}}) }}
    </div>

is that doable from the form builder ?

Upvotes: 0

Views: 76

Answers (1)

JessGabriel
JessGabriel

Reputation: 1082

  • You can use phpoffice/phpexcel to read your Excel files (it will be good to use service) : http://www.techchattr.com/how-to-read-excel-files-with-php
  • Add it to your FormBuilder definitions as params like :

    $data = $options['data'];

  • Pass it as 'choices' of your field

  • Then, pass data as params of formbuilder like ($data contains infos from phpExcel) :

    $form = $this->createForm(YourType::class, $entity, ['data' => $data]);
    

Upvotes: 1

Related Questions