简体   繁体   English

SonataAdmin表单EntityType不需要

[英]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' 
        ])
    ;
}

I've just found that Sonata is adding a little cross to delete the current selected relationship 我刚刚发现奏鸣曲添加了一个小小的十字架以删除当前选择的关系

It's so small that I didn't saw it last night... 它是如此之小,以至于昨晚我没看到它。

在此处输入图片说明

Thanks anyways for the answer M. Kebza! 无论如何,谢谢您的回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM