【问题标题】:symfony Dynamic Generation for Submitted Forms not working提交表单的 symfony 动态生成不起作用
【发布时间】:2016-06-23 06:28:56
【问题描述】:

我按照 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>

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

【问题讨论】:

  • 也许我认为 JavaScript 不工作?我是新手,希望有所帮助,谢谢。
  • 你发现问题了吗?我有另一个问题。 Catchable Fatal Error: Argument 1 passed to AcmeBundle\Form\ScheduleType::PIE10Bundle\Form\{closure}() must be an instance of AcmeBundle\Form\Symfony\Component\Form\Form, instance of Symfony\Component\Form\Form given, called in /var/www/Acme/api/src/AcmeBundle/Form/ScheduleType.php on line 61 and defined有什么想法吗?

标签: php forms symfony doctrine


【解决方案1】:

您没有提交整个表单。试试这个:

$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')
      );
      }
   });
});

【讨论】:

  • 我遇到了一个问题,如果我按照文档进行操作,我会收到错误,因为并非所有数据都已发送。如果我使用这个解决方案(全部发送)一切正常。
猜你喜欢
  • 1970-01-01
  • 2014-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多