我正在尝试将我的Symfony应用程序从2.6 . *更新为2.7 . *并且我在查找新的表单系统以及如何使用它时遇到了麻烦 .

我有一些LazyChoiceLists来加载多个表单使用的常见事物的选择 . 比如美国国家名单 . 这样,如果我需要更改它,我不必深入挖掘20个FormTypes并更改其状态值 .

但是在2.7 . *他们似乎已经取消 choice_list 支持 choice_loader 或只是使用 choices 并传递一系列的callables来修改它 .

我还有一个LazyChoiceList,它根据系统中找到的文件创建一个列表 .

简而言之,我如何将这些移动到新方法?我似乎想通过Change Blog和这个简短的tutorial blog读出来

这是我目前2.6 . *的一个例子 .

<?php
namespace AppBundle\Form\ChoiceList;

use Symfony\Component\Form\Extension\Core\ChoiceList\LazyChoiceList;
use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList;
use Symfony\Component\Finder\Finder;

class TemplateChoice extends LazyChoiceList
{
    public function loadChoiceList()
    {
        $finder = new Finder();

        $finder->files()->in(__DIR__ . '/../../Resources/views/Emails/');

        $choices = array();

        foreach($finder as $file)
        {
            $path = $file->getRealPath();
            $parent = basename($file->getPath());
            $filename = $file->getBaseName('.twig');

            if($parent === 'Emails')
            {
                $choices[$path] = ucfirst($filename);
            }
            else
            {
                $choices[$parent][$path] = '[' . $parent . '] ' . ucfirst($filename);
            }
        }

        return new SimpleChoiceList($choices);
    }
}

此文件中已经进行了一些更改,例如将 setDefaultOptions() 移动到 configureOptions()

<?php

namespace AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class AgentEmailType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', 'text', array('label' => 'Template Name'))
            ->add('emailType' , 'choice' , array(
                'label' => 'Template Type',
                'choices' => array(
                    'confirmation' => 'Confirmation Email',
                    'quote' => 'Quote Email'
                )
            ))
            ->add('fromAddress', 'text', array('label' => 'From Email'))
            ->add('subject', 'text', array('label' => 'Subject Line'))
            ->add('htmlTemplate' , 'choice' , array(
                'label' => 'HTML Template File',
                'choice_list' => new \AppBundle\Form\ChoiceList\TemplateChoice()
            ))
            ->add('plainTemplate' , 'choice' , array(
                'label' => 'Text Template File',
                'choice_list' => new \AppBundle\Form\ChoiceList\TemplateChoice()
            ))
            ->add('agent', 'entity' , array(
                'property' => 'name',
                'class' => 'AppBundle\Entity\Agent',
                'choices' => $options['agents']
            ));
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setRequired('agents');

        $resolver->addAllowedTypes(array(
            'agents' => '\Doctrine\Common\Collections\Collection'
        ));

        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\AgentEmail'
        ));
    }

    public function getName()
    {
        return 'agentEmail';
    }
}

UPDATE 1

我可能应该在加载此特定表单时添加这是我得到的错误 . 至少我非常有信心这种形式是罪魁祸首 .

Catchable Fatal Error: Argument 1 passed to Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory::Symfony\Component\Form\ChoiceList\Factory\{closure}() must be an instance of Symfony\Component\Form\Extension\Core\View\ChoiceView, array given

UPDATE 2

我发现了这个bug report . 这可能是我最初的问题 . 但是,最佳做法是更新这些列表以使用新系统处理,而不是使用已弃用的工具 .