dsfds656545 2018-01-29 14:35
浏览 35
已采纳

即时补充表格

[SETTINGS]

  • Symfony 3.4
  • Package entity : [id, form_id, maretial_id, design_id]
  • Form entity : [id, form_name]
  • Material entity : [id, material_name, form_id]
  • Design entity : [id, design_name, material_id]

[PROBLEM]

I would like to know how to supplement a form with data from an Ajax request.

In my main form, I can select only select the package form (form_id). Once it's selected, via an Ajax request, I will look up for every material objects which are compatible with the selected form:

Form my controller, I'm calling my form builder :

<kbd>FormMaterialsType.php</kbd>

public function buildSupplementForm(FormBuilderInterface $builder, array $options) {
    $builder
        ->add('material', EntityType::class, array(
            'class' => 'AppBundle\Entity\Material',
            'choice_label' => 'Material',
            'query_builder' => function(EntityRepository $er) {
                return $er->createQueryBuilder('m')
                          ->where('m.form= :form');
                          ->setParameters(array(
                              new Parameter('form', $options['form_id']),
                ))
            },
            'empty_data' => null,
            'required' => true)
        );
}

The problem is that I'm getting a full form. How can I get only the select bloc?

  • 写回答

1条回答 默认 最新

  • dongmubi4444 2018-01-29 15:14
    关注

    What you actually need is this

    The main steps are:

    • Listen for a change event on your package select box
    • Submit the form from javascript
    • In your FormType you need to add a subscriber and listen for a POST event on your form
    • Inside your subscriber you must create the functionality so based on the selection in package to query for the right materials
    • After all that is done in your javascript you load the new select box by replacing it with the new one

    Symfony Docs Example:

    $("#material_select_box_id").replaceWith($(html).find('#material_select_box_id'));
    

    So now your material select box will be loaded with the data from the subscriber

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mySQL5.7.34安装遇到的问题
  • ¥15 结构功能耦合指标计算
  • ¥20 visual studio中c语言用ODBC链接SQL SERVER
  • ¥50 AI大模型精调(百度千帆、飞浆)
  • ¥15 非科班怎么跑代码?如何导数据和调参
  • ¥15 福州市的全人群死因监测点死亡原因报表
  • ¥15 Altair EDEM中生成一个颗粒,并且各个方向没有初始速度
  • ¥15 系统2008r2 装机配置推荐一下
  • ¥15 悬赏Python-playwright部署在centos7上
  • ¥15 psoc creator软件有没有人能远程安装啊