Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.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
Php Symfony forms EventListener不返回正确的html结构_Php_Jquery_Ajax_Symfony_Symfony Forms - Fatal编程技术网

Php Symfony forms EventListener不返回正确的html结构

Php Symfony forms EventListener不返回正确的html结构,php,jquery,ajax,symfony,symfony-forms,Php,Jquery,Ajax,Symfony,Symfony Forms,我已经建立了一个被动态修改的表单。我遵循了文档,我有一个问题 我的目标是在用户选择品牌字段时更新模型字段 下面是我的听众的样子: public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('name') ->add('brand', EntityType::class, [ 'class' =

我已经建立了一个被动态修改的表单。我遵循了文档,我有一个问题

我的目标是在用户选择品牌字段时更新模型字段

下面是我的听众的样子:

    public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name')
        ->add('brand', EntityType::class, [
            'class' => 'DEERCMS\BrandBundle\Entity\Brand',
            'choice_label' => 'name',
            'multiple' => false,
            'expanded' => false,
        ]);

    $formModifier = function (FormInterface $form, Brand $brand = null) {
        $models = null === $brand ? array() : $brand->getModels();

        $form->add('model', EntityType::class, array(
            'class'       => 'DEERCMS\ModelBundle\Entity\Model',
            'choices'     => $models,
            'multiple' => false,
            'expanded' => false,
        ));
    };

    $builder->addEventListener(
        FormEvents::PRE_SET_DATA,
        function (FormEvent $event) use ($formModifier) {
            // this would be your entity, i.e. SportMeetup
            $data = $event->getData();
            $formModifier($event->getForm(), $data->getBrand());
        }
    );

    $builder->get('brand')->addEventListener(
        FormEvents::POST_SUBMIT,
        function (FormEvent $event) use ($formModifier) {
            // It's important here to fetch $event->getForm()->getData(), as
            // $event->getData() will get you the client data (that is, the ID)
            $brand = $event->getForm()->getData();

            // since we've added the listener to the child, we'll have to pass on
            // the parent to the callback functions!
            $formModifier($event->getForm()->getParent(), $brand);
        }
    );

}
ajax调用正在发送,但它返回完全相同的html结构,我的目标是更新名为model的字段。以下是模板:

{{ form_start(form, {'attr': {'id': 'form', 'class': 'form-horizontal', 'onChange': 'changed()'  } }) }}
   <div class="form-group">
       <label class="col-md-3 control-label">Marka </label>
          <div class="col-md-9">
              {{form_row(form.brand, {'id': 'test', 'attr': { 'class': 'form-control'}}) }}
           </div>
   </div>
   <div class="form-group">
       <label class="col-md-3 control-label">Model </label>
             <div class="col-md-9">
               {{form_widget(form.model, {'id': 'test1', 'attr': { 'class': 'form-control'}}) }}
              </div>
    </div>

    <div class="form-group">
       <label class="col-md-3 control-label"></label>
           <div class="col-md-9">
                <button type="submit" class="btn btn-success">{% if is_editing %}EDYTUJ {% else %}DODAJ{% endif %}</button>
           </div>
   </div>
{{form_end(form)}}
这里是AJAX调用

  <script>
function changed() {
    var brand = $('#test');
    // ... retrieve the corresponding form.
    var $form = $('#form');

    console.log($form);
    // Simulate form data, but only include the selected sport value.
    var data = {};
    data[brand.attr('name')] = brand.val();
    // Submit data via AJAX to the form's action path.
    jQuery.ajax({
        url : $form.attr('action'),
        type: "POST",
        data: data,
        success: function (html) {
            console.log(html)
            $("#test1").replaceWith(
                // ... with the returned one from the AJAX response.
                $(html).find("#test1")
            );
            // Position field now displays the appropriate positions.
        }
    });
}
 </script>
当我执行console.loghtml时,它显示的是完全相同的html结构,所以它不包含模型数据,应该从侦听器中检索这些数据

我检查了我所有的关系,他们都很好

我自己解决了

它不起作用的原因是我在form_start中更换了一次阿曲布他,它应该在form_rowform.brand中