在這裏,您可以在需要時修改表單。
第一種形式包含的類別,當用戶選擇一個類別,形式是,如果一個類別設置發送(阿賈克斯)和pre_bind檢查的事件,如果它的設置,你添加一個字段的子類
例如:
class CategoryEventSubscriber implements EventSubscriberInterface
{
protected $factory;
public function __construct(FormFactory $factory)
{
$this->factory = $factory;
}
public function preBind(FormEvent $event)
{
$form = $event->getForm();
$data = $event->getData();
$levelOne = isset($data['categoryLevelOne']) ? $data['categoryLevelOne'] : null;
if (!empty($levelOne)) {
$form->add($this->factory->createNamed('categoryLevelTwo'));
}
// level n+1 ...
}
public static function getSubscribedEvents()
{
return array(FormEvents::PRE_BIND => 'preBind');
}
}
在你窗體添加事件訂閱
$builder->addEventSubscriber(new CategoryEventSubscriber($builder->getFormFactory()));
從這裏開始:http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html – cheesemacfly