[英]SonataAdmin form EntityType not required
My entity has an optional relationship ( nullable=true
) to other entity. 我的实体与其他实体具有可选关系(
nullable=true
)。
But When I use required = false
The form created by Sonata has a <select>
with only all my entities, not a blank value. 但是当我使用
required = false
,由Sonata创建的表单只有一个<select>
其中包含所有我的实体,而不是空白值。
With a classic symfony form, required = false
allow to select no entity 使用经典的symfony形式,
required = false
允许不选择任何实体
/**
* @param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('otherEntity', EntityType::class, [
'class' => OtherEntity::class,
'required' => false,
])
;
}
Do you know why? 你知道为什么吗?
First, check if your entity allows for null value on your relationship. 首先,检查您的实体是否允许您的关系中包含空值。 In entity something like (note JoinColumn):
在实体中,例如(请注意JoinColumn):
/**
* @var OtherEntity
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\OtherEntity")
* @ORM\JoinColumn(nullable=true)
*/
private $otherEntity;
Second add placeholder option to your form mapping: 第二个在表单映射中添加占位符选项:
/**
* @param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('otherEntity', EntityType::class, [
'class' => OtherEntity::class,
'required' => false,
// This is what sonata requires
'placeholder' => 'Please select entity'
])
;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.