【发布时间】:2013-12-04 21:20:04
【问题描述】:
在 Symfony2 表单中苦苦挣扎。 User 对象具有类 Location 的属性。表单需要在 2 个选择框中显示位置:国家、城市。 (后面城市选择框会通过ajax更新)。
尝试使用数据转换器和事件,但找不到出路,只是变得更加困惑。 有什么提示可以采取哪些步骤来完成这项工作吗?
// User class
class User
{
...
protected $location;
}
// LOCATION class
class Location
{
...
protected $city;
protected $country;
}
// User TYPE
class UserType extends AbstractType
{
...
public function buildForm(FormBuilderInterface $builder, array $options)
{
...
$builder->add("location", new LocationType);
}
}
// CUSTOM Location FORM TYPE
class LocationType extends AbstractType
{
....
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add("country", "choice");
$builder->add("city", "choice");
}
}
【问题讨论】:
-
对不起,错误在哪里?因为这么多的代码,我看不出有什么问题。
-
没有错误。我需要一些提示以使其发挥作用。
-
symfony.com/doc/current/cookbook/form/form_collections.html 这是关于你在做什么的文档,嵌入表单?不是吗?
-
Location 实体生成国家和城市
choice字段,这表明应该有一个可用选项列表(即城市和国家实体)。将它们作为文本框不是更有意义吗?或者你打算限制位置?位置应按照@Jahnux73 的建议处理。
标签: php symfony symfony-forms