我是一位有用的助手,可以为您翻译文本。
我有一个表单,其中有1个实体类型字段,必须包含取决于第二个未映射在第一个实体中的实体类型字段的选择项,就像这样:
ServicePlaceType.php:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('placetype', EntityType::class, array(
"class" => "AppBundle:PlaceType",
"choice_label" => "place",
"mapped" => false
))
->add('idplace', EntityType::class, array(
"class" => "AppBundle:Place",
"choice_label" => "place"
))
->add('...');
表格
+---------+--------------+---------------+-----------+
| Service | ServicePlace | Place | PlaceType |
+---------+--------------+---------------+-----------+
| | id | | |
+---------+--------------+---------------+-----------+
| | idplace > | < id | |
+---------+--------------+---------------+-----------+
| id > | < idservice | idPlaceType > | < id |
+---------+--------------+---------------+-----------+
| service | | place | placetype |
+---------+--------------+---------------+-----------+
所以,当我选择一个 PlaceType 时,我希望 Place 选择器仅显示 idplacetype 匹配 PlaceType id 的地点。
我尝试使用 javascript,在 PlaceType 选择器上的 onChange 事件中根据 PlaceType 的实际值过滤 Place 选项,但我不知道如何在 formType 中获取 Place 的 PlaceType 属性。 我尝试了那种东西,但它不起作用。
->add('idplace', EntityType::class, array(
"class" => "AppBundle:Place",
"choice_label" => "place",
"attr" => array("placeType" => $this->getPlaceType()), // nor like that
))
->add('idplace', EntityType::class, array(
"class" => "AppBundle:Place",
"choice_label" => "place",
"attr" => array("placeType" => function ($place) {
return $place->getPlaceType();
}), // neither like that
))
有人知道如何获取这些数据吗?或者如何以另一种方式动态过滤选项?
感谢您的帮助!