在Symfony admin中,我有一个表单,其中第二个字段类型取决于所选的字段值。第二个字段可以是Symfony Url字段类型或奏鸣曲提供sonata_type_model_list字段类型。在Controller中使用Sonata字段类型创建表单
我已经创建了一个ajax请求到My Bundle Controller来返回包含所需字段的表单。
> /src/MyBundle/Controller/MyController.php
namespace MyBundle\Controller
use Sonata\AdminBundle\Controller\CRUDController;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Sonata\AdminBundle\Form\FormMapper;
class MyController extends CRUDController
{
public function getFieldAction()
{
//getting the value of choice field
$type = $this->get('request')->get('type');
//sonata.admin.reference is a service name of ReferenceBundle admin class
$fieldDescription = $this->admin->getModelManager()
->getNewFieldDescriptionInstance($this->admin->getClass(), 'reference');
$fieldDescription->setAssociationAdmin($this->container->get('sonata.admin.reference'));
$fieldDescription->setAdmin($this->admin);
$fieldDescription->setAssociationMapping(array(
'fieldName' => 'reference',
'type' => ClassMetadataInfo::ONE_TO_MANY,
));
// Getting form mapper in controller:
$contractor = $this->container->get('sonata.admin.builder.orm_form');
$mapper = new FormMapper($contractor, $this->admin->getFormBuilder(), $this->admin);
$form_mapper = $mapper->add('reference', 'sonata_type_model_list', array(
'translation_domain' => 'ReferenceBundle',
'sonata_field_description' => $fieldDescription,
'class' => $this->container->get('sonata.admin.reference')->getClass(),
'model_manager' => $this->container->get('sonata.admin.reference')->getModelManager(),
'label' => 'Reference',
'required' => false,
));
//@ToDo build $form from $form_mapper
return $this->render('MyBundle:Form:field.view.html.twig', array(
'form' => $form->createView(),
));
}
}
我无法找到索纳塔\ AdminBundle \表格\ FormMapper类中的任何方法来建立一个表(它似乎是可能的创建()方法,但它仅与普通的Symfony字段类型的作品,不奏鸣曲表单字段类型,通常在Block或Admin类中生成)。
是否有可能使用索纳塔\ AdminBundle \表格\ FormMapper在控制器建立一个形式? 或者还有另一种方式,我可以用控制器中的奏鸣曲表单字段类型构建表单吗?
为什么不使用您的管理员类别制作表格?这是为了这个 – chalasr