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

如何在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;
        }
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题