Symfony3 Ajax表单添加额外字段

时间:2016-08-11 06:25:55

标签: jquery symfony ajaxform symfony-3.1

AdminBundle \表格\ CoreForm.php:

class CoreForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        /**
        Ajax Call : onSubmitAdd
        */
        $builder->add('set_core_doctrine_metadata_type', ChoiceType::class, array(
            'label' => 'Doctrine Metadata Cache Driver',
            'choices' => $this->getDoctrineAccelerator(),
            'empty_data' => 'array',
            'choice_translation_domain' => false,
            'attr' => array(
                'onSubmitAdd' => 'set_core_doctrine_metadata_host|set_core_doctrine_metadata_port'
            )
        ))

        /**
        Ajax Response : onSubmitAdd
        */
        $doctrineModify = function (FormInterface $form){
            $metaDataDriver = $form->get('set_core_doctrine_metadata_type')->getData();

            if (!$form->isValid()) {
                if ($metaDataDriver == 'memcache' || $metaDataDriver == 'memcached'){
                    $form->add('set_core_doctrine_metadata_host', TextType::class, array(
                        'label' => 'Doctrine Metadata Cache Host',
                    ))
                    ->add('set_core_doctrine_metadata_port', IntegerType::class, array(
                        'label' => 'Doctrine Metadata Cache Port',
                    ));
                }
            }
        };

        $builder->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) use ($doctrineModify){
            $form = $event->getForm();
            $doctrineModify($form);
        });
    }
}

表格类型: 形式工作顺利。但它不允许新的领域。

jQuery Ajax代码:

$(document).on('change', '[onSubmitAdd]', function () {
    // Get Form
    form = $(this).closest('form');

    // Send Data
    var data = {};
    data[$(this).attr('name')] = $(this).val();

    // Html Block
    formGroup = $(this).closest('.form-group');
    getFormGroup = $(this).attr('onSubmitAdd');
    getFormGroup = getFormGroup.split('|');

    $.ajax({
        url: form.attr('action'),
        type: form.attr('method'),
        data: data,
        success: function (data) {
            addContent = "";

            // Get Items
            $.each(getFormGroup, function (index, item) {
                dt = $(data.content.body).find('.' + item).html();
                if (typeof dt != 'undefined'){
                    addContent = addContent + '<div class="form-group">'+ dt +'</div>';
                }
            });

            // Replace or Add
            if (formGroup.next().hasClass('onSubmitAdded')){
                formGroup.next().html(addContent);
            } else {
                formGroup.after('<div class="onSubmitAdded">'+ addContent +'</div>');
            }
        }
    });
});

问题: Ajax系统运行顺畅。 但是在提交表单时,错误代码“此表单不应包含额外字段”

1 个答案:

答案 0 :(得分:1)

为了能够接受额外提交的选项,您必须在PRE_SUBMIT Event上的表单中添加它们:

 class FooType extends AbstractType
 {
     public function buildForm(FormBuilderInterface $builder, array $options)
     {
         $builder
             ->add('tag', ChoiceType::class, array('choices'=>array()))
         ;

         $builder->addEventListener(
             FormEvents::PRE_SUBMIT,
             function(FormEvent $event){
                 // Get the parent form
                 $form = $event->getForm();

                 // Get the data for the choice field
                 $data = $event->getData()['tag'];

                 // Collect the new choices
                 $choices = array();

                 if(is_array($data)){
                     foreach($data as $choice){
                         $choices[$choice] = $choice;
                     }
                 }
                 else{
                     $choices[$data] = $data;
                 }

                 // Add the field again, with the new choices :
                 $form->add('tag', ChoiceType::class, array('choices'=>$choices));
             }
         );
     }
 }

要避免“Notice: Array to string conversion”想要以$ form格式复制新字段中的所有字段数组参数,例如,如果您有'multiple'=&gt; $ builder'标记中的 >