我创建了一个带有查询生成器的 EntityType 字段的表单:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$entity = $builder->getData();
$relatedParentId = $entity->getParentEntity()->getId();
$builder->add('myEntity', EntityType::class, array(
'class' => myEntity::class,
'query_builder' => function(AppBundleRepositorymyEntityRepository $eR) use ($relatedEntityId) {
return $aR->getByParentId($relatedEntityId);
},
))
}
当查询返回值时,它很好。但是,当返回 null 时,它在表单上显示为只是一个空白选项,下拉列表为空,并且不是很用户友好。我想有一条消息说没有返回任何结果。
请注意,当返回结果时,不需要任何消息,因此"占位符"选项是不够的。我可能错过了文档中明显的东西,但到目前为止我没有运气。
使用占位符选项:
$builder->add('states', EntityType::class, array(
'class' => myEntity::class,
'query_builder' => function(AppBundleRepositorymyEntityRepository $eR) use ($relatedEntityId) {
return $aR->getByParentId($relatedEntityId);
}
'placeholder' => $hasResults ? false : 'No results',
));
你之前在哪里构建 hasResult 标志。