提交表单的symfony动态生成不起作用

时间:2016-06-23 06:28:56

标签: php forms symfony doctrine

我按照symfony文档http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#dynamic-generation-for-submitted-forms为依赖表单进行动态生成。

类型类:

class EtlType extends AbstractType
{

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('outTable', EntityType::class, array(
                'class'=>'TableBundle:TableList',
                'placeholder' => '',
                'choice_label'=>'tableName',
                'attr' => array('class' => 'form-control', 'style' => 'margin-bottom:10px; width:180px')));

        $formModifier = function (FormInterface $form, TableList $tableList = null) {
            $fields = null === $tableList ? array() : $tableList->getFields();
            $form->add('outField', EntityType::class, array(
                'class'       => 'TableBundle:TableDesc',
                'placeholder' => '',
                'choices'     => $fields,
            ));
        };

        $builder->addEventListener(
            FormEvents::PRE_SET_DATA,
            function (FormEvent $event) use ($formModifier) {
                $data = $event->getData();
                $formModifier($event->getForm(), $data->getOutTable());
            }
        );

        $builder->get('outTable')->addEventListener(
            FormEvents::POST_SUBMIT,
            function (FormEvent $event) use ($formModifier) {
                $outTable = $event->getForm()->getData();
                $formModifier($event->getForm()->getParent(), $outTable);
            }
        );
    }
}

树枝模板:

                    {{ form_start(form) }}
                    {{ form_row(form.outTable) }}
                    {{ form_row(form.outField) }}
                    {{ form_widget(form) }}
                    <input class="button button1 right" type="submit" value="Create" onclick="return confirm('Confirm Creation?')"/>
                    {{ form_end(form) }}
                    <script>
                        var $outTable = $('#etl_outTable');
                        $outTable.change(function() {
                            // ... retrieve the corresponding form.
                            var $form = $(this).closest('form');
                            // Simulate form data, but only include the selected outTable value.
                            var data = {};
                            data[$outTable.attr('tableName')] = $outTable.val();
                            // Submit data via AJAX to the form's action path.
                            $.ajax({
                                url : $form.attr('action'),
                                type: $form.attr('method'),
                                data : data,
                                success: function(html) {
                                    // Replace current position field ...
                                    $('#etl_outField').replaceWith(
                                            // ... with the returned one from the AJAX response.
                                            $(html).find('#etl_outField')
                                    );
                                }
                            });
                        });
                    </script>

我想我按照文档中的步骤操作。但对于outField,它总是没有什么可选择的。有人可以指出出了什么问题吗?非常感谢。

1 个答案:

答案 0 :(得分:0)

你不提交整个表格。试试这个:

$outTable.change(function() {
   // ... retrieve the corresponding form.
   var $form = $(this).closest('form');
   // Simulate form data, and include the whole form.
   var data = new FormData($form);
   // Submit data via AJAX to the form's action path.
   $.ajax({
      url : $form.attr('action'),
      type: $form.attr('method'),
      data : data,
      success: function(html) {
         // I'd rather replace the whole form
         $('#etl_outField').replaceWith(
         // ... with the returned one from the AJAX response.
         $(html).find('#etl_outField')
      );
      }
   });
});