0

我最近刚刚更新到 2.8,现在调用 Form Factory 的 create 函数时出现以下错误。

Error: Class Symfony\Component\Form\Extension\Core\Type\FormType contains 1 
abstract method and must therefore be declared abstract or implement the 
remaining methods (Symfony\Component\Form\FormTypeInterface::setDefaultOptions)

FormFactory 的调用如下所示:

$this->formFactory->create(
        get_class(new ProductType()),
        $product,
        [
            'method' => 'POST',
            'type' => $type,
            'locales' => $context->shop->getLocales(),
            'product' => $product,
            'numberDataTransformer' => $this->numberTransformerFactory->createFromLocale(
                $context->user->getLocale(),
                $context->shop->getDefaultLocale()
            ),
            'priceType' => $context->shop->getConfiguration()->getProductConfiguration()->getPricesBackend(),
            'isShortDescriptionEnabled' => $context->shop->getConfiguration()->getProductConfiguration()->isShortDescriptionEnabled()
        ]);

我尝试了几种方法将 ProductType 传递给函数,但似乎都没有。我总是得到两个结果之一:结果是找不到类型,或者返回 FormType 没有实现 setDefaultOptions 的错误。

我错过了什么?

编辑:

以下是一些附加代码:

formFactory参数的声明:

public function __construct(Request $request, FormFactoryInterface $formFactory)
{
    $this->request = $request;
    $this->formFactory = $formFactory;
}

ProductType 类

<?php

namespace CustomNamespace\BackendBundle\Product\Form;

use CustomNamespace\BackendBundle\Common\NumberDataTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use CustomNamespace\BackendBundle\Product\Form\ImageType;
use ShopwareEasy\BackendBundle\Product\Form\AttachmentType;
use Symfony\Component\Validator\Exception\InvalidOptionsException;

/**
 * Form element type for products.
 */
class ProductType extends AbstractType
{
    /**
     * @var string
     */
    private $method;

    /**
     * @var string
     */
    private $type;

    /**
     * @var array
     */
    private $locales;

    /**
     * @var Product
     */
    private $product;

    /**
     * @var \CustomNamespace\BackendBundle\Common\NumberDataTransformer
     */
    private $numberDataTransformer;

    /**
     * @var string
     */
    private $priceType;

    /**
     * @var bool
     */
    private $isShortDescriptionEnabled;

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     * @return void
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        $builder->setMethod($this->method);
        $regionType = new RegionShippingTimeType();

        if ($this->type == 'download') {
            $regionType->enableForDownloadableProduct();
        }

        $builder->add('regions', 'collection', array(
            'type'   => $regionType,
            'label' => false,
            'options'  => array(
                'required'  => false,
                'attr'      => array('class' => 'email-box')
            ),
        ));

        $builder->add('vendor', 'text', ['label' => 'form_product_vendor']);
        if ($this->type == 'normal') {
            $builder->add(
                'mainVariant',
                new MainVariantNormalType(
                    $this->method,
                    $this->locales,
                    $this->product,
                    $this->numberDataTransformer,
                    $this->priceType,
                    $this->isShortDescriptionEnabled
                ),
                ['error_bubbling' => false, 'label' => false]
            );
        } elseif ($this->type == 'download') {
            $builder->add(
                'mainVariant',
                new MainVariantDownloadType(
                    $this->method,
                    $this->locales,
                    $this->product,
                    $this->numberDataTransformer,
                    $this->priceType,
                    $this->isShortDescriptionEnabled
                ),
                ['error_bubbling' => false, 'label' => false]
            );
        } elseif ($this->type == 'variant') {
            $builder->add(
                'mainVariant',
                new MainVariantVariantType(
                    $this->method,
                    $this->locales,
                    $this->product,
                    $this->numberDataTransformer,
                    $this->priceType,
                    $this->isShortDescriptionEnabled
                ),
                ['error_bubbling' => false, 'label' => false]
            );
        }

        if ($this->method == 'PUT') {
            $builder->add(
                'images',
                new ImageType(),
                ['error_bubbling' => true, 'label' => false]
            );

            $builder->add(
                'attachments',
                new AttachmentType(),
                ['error_bubbling' => true, 'label' => false]
            );
        }
    }

    /**
     * @param \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver
     */
    public function configureOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(
            array(
                'data_class' => 'CustomNamespace\\BackendBundle\\Product\\Product',
                'csrf_protection' => false,
                'error_bubbling' => false,
                'cascade_validation' => true,
                'method' => 'POST',
                'type' => 'normal'
            )
        );

        parent::configureOptions($resolver);
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        /** @var OptionResolver $resolver */
        $this->configureOptions($resolver);
    }

    public function getName() {
        return get_class($this);
    }
}
4

1 回答 1

0

问题是由根本没有更新的文件产生的。 setDefaultOptions 的实现应该仍然存在于 Symfony 2.8 类中——但他们没有。在摧毁我的流浪者并重新创建它之后,一切都很好。

但是感谢大家的帮助!

于 2015-12-22T16:00:23.477 回答