Symfony3,如何在表单中加载外部选项列表?

时间:2016-01-09 21:20:04

标签: php forms symfony

我不知道我是在面对一个错误还是做错了什么。我正在尝试从自定义类加载ChoiceType表单的选项,但是我收到以下错误:

Catchable Fatal Error: Argument 1 passed to Symfony\Component\Form\ChoiceList\LazyChoiceList::__construct() must be an instance of Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface, none given

我的自定义课程:

<?php
namespace AppBundle\Form\ChoiceLists;

use Symfony\Component\Form\ChoiceList\SimpleChoiceList;
use Symfony\Component\Form\ChoiceList\LazyChoiceList;

class DepartmentsChoiceList extends LazyChoiceList
{
    public function loadChoiceList()
    {
         $choices = array(
             '01' => "Ain",
             '02' => "Aisne",
             '03' => "Allier",
             '04' => "Alpes de Haute Provence",
             '05' => "Alpes (Hautes)",
             //....
             '94' => "Val de Marne",
             '95' => "Val d&acute;Oise",
             '98' => "Mayotte",
             '9A' => "Guadeloupe",
             '9B' => "Guyane",
             '9C' => "Martinique",
             '9D' => "R&eacute;union",
             );
         return new SimpleChoiceList($choices);
    }
}

在我的formType中:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('birthDepartment', ChoiceType::class, array(
            'label' => 'Département :',
            'required' => false,
            'choices' => new DepartmentsChoiceList(),
         )
     );

我发现此功能的文档很少。

0 个答案:

没有答案