简体   繁体   English

Sonata Admin Bundle - configureFormFields中的不同字段选项

[英]Sonata Admin Bundle - Different field options in configureFormFields

是否有可能在Sonata Admin Bundle Form中添加不同的字段,具体取决于您是在创建新实体还是在configureFormFields中编辑现有实体?

I'm not sure if this is the best way, but I've accomplished this using: 我不确定这是否是最好的方法,但是我已经使用以下方法完成了此操作:

protected function configureFormFields(FormMapper $form)
{
    // Add fields common to add AND edit...

    if ($this->getSubject()->getId() > 0) {
        // Add fields only when editing an existing object
    }
}

Obviously you could add an else condition too if you want to only be able to add fields for a new object. 显然,如果您只想为新对象添加字段,则也可以添加else条件。

Here is a better way witch is recommended by the officiel documentation ( Click here ) 这是官方文档中推荐女巫的一种更好的方法( 单击此处

$subject = $this->getSubject();

if ($subject->isNew()) {
    $formMapper->add('customField', TextType::class);
}

or you can do: 或者你可以这样做:

if ($this->isCurrentRoute('create')) {
    $formMapper->add('name', TextType::class);
}

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

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