我想在symfony中制作一些简单的管理面板应用程序。我看到,由于版本2.3 symfony引入了bootstrap的表单主题,这很好,但我想创建名为savetype的自定义提交字段,该字段应该将默认类attr设置为btn primary,而不是btn default。
因此,从documentation我读到我可以创建自定义字段类型并将其父字段设置为SubmitType
存储类型自定义字段

<?php

namespace AppBundle\Form\Custom;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * Class SaveType
 * @package AppBundle\Form
 */
class SaveType extends AbstractType
{
    /**
     * @param OptionsResolver $resolver
     * @return OptionsResolver|void
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        return $resolver->setDefaults(
            [
                'attr' => [
                    'class' => 'btn-primary',
                ],
            ]
        );
    }

    /**
     * @return string
     */
    public function getParent()
    {
        return SubmitType::class;
    }
}

产品实体
<?php

namespace AppBundle\Entity;

/**
 * Product
 */
class Product
{
    /**
     * @var int
     */
    private $id;

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

    /**
     * @var int
     */
    private $price;

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


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Product
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set price
     *
     * @param integer $price
     *
     * @return Product
     */
    public function setPrice($price)
    {
        $this->price = $price;

        return $this;
    }

    /**
     * Get price
     *
     * @return int
     */
    public function getPrice()
    {
        return $this->price;
    }

    /**
     * Set enabled
     *
     * @param boolean $enabled
     *
     * @return Product
     */
    public function setEnabled($enabled)
    {
        $this->enabled = $enabled;

        return $this;
    }

    /**
     * Get enabled
     *
     * @return bool
     */
    public function getEnabled()
    {
        return $this->enabled;
    }
}

使用savetype的producttype
<?php

namespace AppBundle\Form;

use AppBundle\Form\Custom\SaveType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * Class ProductType
 * @package AppBundle\Form
 */
class ProductType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('price', MoneyType::class, [
                'currency' => 'PLN',
                'divisor' => 100,
            ])
            ->add('enabled')
            ->add('submit', SaveType::class, [
                'label' => 'Save',
            ]);
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Product',
        ));
    }
}

问题
呈现该窗体将引发以下错误:
Neither the property "submit" nor one of the methods "getSubmit()", "submit()", "isSubmit()", "hasSubmit()", "__get()" exist and have public access in class "AppBundle\Entity\Product".

注意
创建submittypeextension工作正常,但我不想更改整个应用程序中标准submittype的行为。

最佳答案

解决方案实际上非常简单,正如我在this issue中发现的那样。您必须实现SubmitButtonTypeInterface以便它使用SubmitButtonBuilder而不是常规的FormBuilder
这就是按钮类的外观:

namespace AppBundle\Form\Custom;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\SubmitButtonTypeInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class SaveType extends AbstractType implements SubmitButtonTypeInterface
{
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'label' => ' ',
            'attr' => [
                'title' => 'Save',
                'data-toggle' => "tooltip",
                'data-placement' => "bottom"
            ],
            'icon' => 'ok'
        ]);
    }

    public function getParent()
    {
        return SubmitType::class;
    }
}

08-19 03:45