如何在 Symfony 表单中创建作为 SubmitType 子级的自定义 SaveType
How to create a custom SaveType which is child of SubmitType in Symfony forms
我想在 Symfony 中制作一些简单的管理面板应用程序。我看到自版本 2.3 Symfony 引入了一个 Bootstrap 的表单主题,这很棒,但我想创建名为 SaveType 的自定义提交字段,它应该将默认 class attr 设置为 btn-primary 而不是btn-默认。
所以,从 documentation I read that I can create that custom field type and set its parent to SubmitType
SaveType 自定义字段
<?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
.
这就是您按钮 class 的样子:
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;
}
}
我想在 Symfony 中制作一些简单的管理面板应用程序。我看到自版本 2.3 Symfony 引入了一个 Bootstrap 的表单主题,这很棒,但我想创建名为 SaveType 的自定义提交字段,它应该将默认 class attr 设置为 btn-primary 而不是btn-默认。
所以,从 documentation I read that I can create that custom field type and set its parent to SubmitType
SaveType 自定义字段
<?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
.
这就是您按钮 class 的样子:
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;
}
}