我有以下复合形式,我想将该位置作为模型/对象传递给LocationType
,我如何在buildForm
内做同样的事情?
如果这是一个常规控制器,那么我会这样做:
$registrationForm = $this->createForm(new ShopRegistrationFormType(), $location, array('label' => false, 'required' => false));
但我怎样才能在FormType
中执行相同的操作:
class ShopRegistrationFormType extends AbstractType
{
private $location;
public function __construct( $location = null)
{
$this->location = $location;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('location', new LocationType())
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'cascade_validation' => true,
));
}
public function getName()
{
return 'shop_registration';
}
}
答案 0 :(得分:1)
尝试通过'数据'添加模型/对象属性:
class ShopRegistrationFormType extends AbstractType
{
private $location;
public function __construct( $location = null)
{
$this->location = $location;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('location', new LocationType(), array(
'data' => $this->location,
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'cascade_validation' => true,
));
}
public function getName()
{
return 'shop_registration';
}
}
希望它有所帮助。