Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ajax 使用Sonata字段类型在控制器中创建窗体_Ajax_Symfony_Controller_Symfony Forms_Sonata Admin - Fatal编程技术网

Ajax 使用Sonata字段类型在控制器中创建窗体

Ajax 使用Sonata字段类型在控制器中创建窗体,ajax,symfony,controller,symfony-forms,sonata-admin,Ajax,Symfony,Controller,Symfony Forms,Sonata Admin,在Symfony admin中,我有一个表单,其中第二个字段类型取决于所选的ChoiceField值。第二个字段可以是SymfonyUrl字段类型,也可以是sonata提供的sonata_type_model_list字段类型 我已经向我的Bundle控制器创建了一个ajax请求,以返回表单,其中包含所需字段 > /src/MyBundle/Controller/MyController.php namespace MyBundle\Controller use Sonata\Admi

在Symfony admin中,我有一个表单,其中第二个字段类型取决于所选的
ChoiceField
值。第二个字段可以是SymfonyUrl字段类型,也可以是sonata提供的sonata_type_model_list字段类型

我已经向我的Bundle控制器创建了一个ajax请求,以返回表单,其中包含所需字段

> /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(),
    ));
  }
}
我在
Sonata\AdminBundle\Form\FormMapper
类中找不到任何方法来构建表单(似乎可以使用
create()
方法,但它只适用于常见的Symfony字段类型,而不适用于Sonata表单字段类型,通常在块或管理类中生成)

是否可以在控制器中使用
Sonata\AdminBundle\Form\FormMapper
构建表单?
或者有没有其他方法可以在控制器中使用Sonata表单字段类型构建表单?

您不应该使用控制器,而应该使用Sonata管理服务

Sonata为您提供了“Sonata类型选择字段掩码”类型,允许您根据此“Sonata类型选择字段掩码”输入的值动态更改窗体上显示的字段

在那里你可以找到关于奏鸣曲类型和选择场面具的一切

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('reference', 'sonata_type_choice_field_mask', array(
            'choices' => array(
                //The list of available 'Reference' here
                'choice1',
                'choice2'
            ),
            'map' => array(
                //What you want to display depending of the selected option
                'choice1' => array(
                    // List of the fields displayed if choice 1 is selected
                    'field1', 'field3'
                ),
                'choice2' => array(
                    // List of the fields displayed if choice 2 is selected
                    'field2', 'field3'
                )
            ),
            'placeholder' => 'Choose an option',
            'required' => false
        ))
        ->add('field1', 'sonata_type_model_list', array(/* Options goes here */))
        ->add('field2', 'url', array(/* Options goes here */))
        ->add('field3')
    ;
}

您不应该使用控制器,而应该使用Sonata管理服务

Sonata为您提供了“Sonata类型选择字段掩码”类型,允许您根据此“Sonata类型选择字段掩码”输入的值动态更改窗体上显示的字段

在那里你可以找到关于奏鸣曲类型和选择场面具的一切

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('reference', 'sonata_type_choice_field_mask', array(
            'choices' => array(
                //The list of available 'Reference' here
                'choice1',
                'choice2'
            ),
            'map' => array(
                //What you want to display depending of the selected option
                'choice1' => array(
                    // List of the fields displayed if choice 1 is selected
                    'field1', 'field3'
                ),
                'choice2' => array(
                    // List of the fields displayed if choice 2 is selected
                    'field2', 'field3'
                )
            ),
            'placeholder' => 'Choose an option',
            'required' => false
        ))
        ->add('field1', 'sonata_type_model_list', array(/* Options goes here */))
        ->add('field2', 'url', array(/* Options goes here */))
        ->add('field3')
    ;
}

看起来您可以访问管理员,管理员直接使用
Sonata\AdminBundle\admin\AbstractAdmin::getForm
构建表单

$form = $this->admin->getForm();

看起来您可以访问管理员,管理员直接使用
Sonata\AdminBundle\admin\AbstractAdmin::getForm
构建表单

$form = $this->admin->getForm();

为什么不使用Admin类制作表单?这就是为什么不使用Admin类制作表单?它就是为这个而来的