【发布时间】:2013-06-19 09:44:57
【问题描述】:
我创建了一个 FormType,它添加了这样的国家/地区字段类型
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('address', 'text', array(
'required' => false,
))
->add('country', 'country', array(
'required' => false,
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => $this->dataClass,
));
}
public function getName()
{
return 'form_contact';
}
我将此表单的 data_class 选项设置为具有 address 和 country 这两个字段的实体都是 string 输入
重新编辑表单并提交就可以了,POST参数包括
form_contact['address'] = 'Some value'
form_contact['country'] = 'US'
但是当我从提交的表单中保留实体时,国家字段为 NULL。转储 $form->getData() 我得到了这个:
object(Namspace\Entity\MyEntity)#3501 (3) {
["id":protected]=>
NULL
["address1":protected]=>
string(12) "Some value"
["country":protected]=>
NULL
["US"]=>
string(2) "US"
}
我希望这个国家是美国而不是关键和价值美国的新元素。你能帮助我吗?非常感谢
【问题讨论】:
-
我发现这是我的错误。在我的实体国家设置器中。应该是 $this->country = $country 而不是 $this->$country = $country 会导致问题。感谢您的帮助 nifr
-
如果您认为这对将来的某人有帮助,请在您的解决方案中发布答案并接受它。
标签: symfony