dongliming2416 2016-02-25 10:33
浏览 61
已采纳

如何在Symfony表单中创建自定义SaveType,它是SubmitType的子类

I want to make some simple admin panel application in Symfony. I see that since version 2.3 Symfony introduced a Bootstrap's form theming, which is great, but I want to create custom submit field called SaveType which should have default class attr set to btn-primary instead of btn-default.

So, from documentation I read that I can create that custom field type and set its parent to SubmitType

SaveType custom field

<?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;
    }
}

Product entity

<?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;
    }
}

ProductType which uses SaveType

<?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',
        ));
    }
}

The problem

Rendering that form throws following error:

Neither the property "submit" nor one of the methods "getSubmit()", "submit()", "isSubmit()", "hasSubmit()", "__get()" exist and have public access in class "AppBundle\Entity\Product".

Note

Creating a SubmitTypeExtension is working fine, but I don't want to change behavior of standard SubmitType in the whole application.

  • 写回答

1条回答 默认 最新

  • doumiebiao6827 2016-11-01 16:25
    关注

    The solution is actually very simple, as I found out with this issue. You have to implement the SubmitButtonTypeInterface so it uses SubmitButtonBuilder instead of the regular FormBuilder.

    This is what you button class will look like:

    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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 angular开发过程中,想要读取模型文件,即图1的335行,会报404错误(如图2)。但我的springboot里配置了静态资源文件,如图3。且在该地址下我有模型文件如图4,请问该问题该如何解决呢?
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置