[英]Custom select in a form type in Symfony3
I am creating a select that takes the data of an entity, called category. 我正在创建一个选择,该选择接受实体的数据,称为类别。
The select that I want to develop, would basically be the same that I have developed and working, but with the values I take from the category entity. 我想要开发的选择基本上与我已经开发和使用的选择相同,但是具有我从类别实体获取的值。
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use BackendBundle\Entity\Categoria;
use BackendBundle\Entity\CategoriaRepository;
class ProductoType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nombre', TextType::class, array(
'label' => 'Nombre',
'required' => 'required',
'attr' => array(
'class' => 'form-name form-control'
)
))
->add('categoria', ChoiceType::class, array(
'choices' => array(
'General' => '1',
'Coffe' => '2'
),
'required' => false,
'empty_data' => null
))
->add('Save', SubmitType::class, array(
"attr" =>array(
"class" => "form-submit btn btn-success"
)
))
;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'BackendBundle\Entity\Producto'
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'backendbundle_producto';
}
}
I would add a section like the following, but I get the error Could not load type "entity" 我将添加以下内容,但出现错误无法加载类型“实体”
->add('categoria', 'entity', array(
'class' => 'BackendBundle:Categoria'
)
)
The original BBDD is documented in Object of class \\BackendBundle\\Entity\\Categoria could not be converted to string 原始BBDD记录在\\ BackendBundle \\ Entity \\ Categoria类的Object中,无法转换为字符串
'entity' should be EntityType::class, you should use the classname of the EntityType instead of just the 'entity' string 'entity'应该是EntityType :: class,您应该使用EntityType的类名而不是'entity'字符串
See: https://github.com/symfony/symfony/blob/master/UPGRADE-3.0.md#form 参见: https : //github.com/symfony/symfony/blob/master/UPGRADE-3.0.md#form
Firstly if your are using symfony 3 you must use Symfony\\Bridge\\Doctrine\\Form\\Type\\EntityType
and the class should be the class name not the entity name 首先,如果您使用的是symfony 3,则必须使用Symfony\\Bridge\\Doctrine\\Form\\Type\\EntityType
,并且该类应该是类名而不是实体名
->add('categoria', 'Symfony\Bridge\Doctrine\Form\Type\EntityType', array(
'class' => 'BackendBundle\Entity\Categoria'
)
)
and categoria should look like: 和类别应该看起来像:
namespace BackendBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table()
* @ORM\Entity()
*/
class Categoria
{
/**
* @var int
*
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string")
*/
protected $name;
public function __toString()
{
return $this->name;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.