首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony 4依赖下拉选择型

Symfony 4依赖下拉选择型
EN

Stack Overflow用户
提问于 2022-10-24 04:13:12
回答 1查看 27关注 0票数 0

我试图在Symfony 4中做出依赖的选择类型。我想在选择环形状时激活环类型,但是我发现即使选择了环形状,环类型也没有激活。我将占位符放置在环型中,因此一旦选择了环形状,就可以更改占位符文本。但它也没有改变。由于环形本身是工作的,我想是缺少一些东西来连接环形状和环类型,但我不知道如何解决这个问题。

这是我的表格

代码语言:javascript
复制
/**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $finder = new Finder();
        $finder->directories()->in('html/upload/save_image/CustomRing/ring')->depth('==0');
        $dir_array = [];
        if($finder->hasResults()){
            foreach($finder as $dir){
                array_push($dir_array, $dir->getFilename());
            }
            
            $dir_array = array_combine($dir_array, $dir_array);
        }
        natsort($dir_array);
        
        
        
        $product = $this->customProductRepository->customProductFindByName();
        $productId = $product[0]->getId();
        $builder
        ->add('product_id', HiddenType::class,['data'=> $productId])
        ->add('ring_name', TextType::class)
        ->add('ring_shape', ChoiceType::class, array(
            'choices' => $dir_array,
            'placeholder' => 'test'
            ))
        ->add('price', IntegerType::class)
        
        ->add('save', SubmitType::class, ['label' => 'Save']);

        $builder->addEventListener(
            FormEvents::PRE_SET_DATA,
            function (FormEvent $event){
                $data = $event->getData();
                //dump($data->getRingShape());
                $ring_shape = $data->getRingShape();
                $this->addRingTypeField($event->getForm(), $ring_shape);
            }
        );
        $builder->get('ring_shape')->addEventListener(
            FormEvents::POST_SUBMIT,
            function (FormEvent $event){
                dump('dd');
                $ring_shape = $event->getForm()->getData();
                
                $this->addRingTypeField($event->getForm()->getParent(), $ring_shape);
            }
        );

    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Ring::class,
        ]);
    }

    private function getRingTypeChoices(string $ring_shape):array
    {
        $finder = new Finder;
        $path = 'html/upload/save_image/CustomRing/ring/'.$ring_shape.'/';
        $finder->directories()->in($path)->depth('==0');
        $dir_array = [];
        if($finder->hasResults()){
            foreach($finder as $dir){
                array_push($dir_array, $dir);
            }
            $dir_array = array_combine($dir_array, $dir_array);
        }
        
        return $dir_array;
    }

    public function addRingTypeField(FormInterface $form, ?string $ring_shape){
        dump($ring_shape);
        $ring_type_choices = null === $ring_shape ? [] : $this->getRingTypeChoices($ring_shape);
        
        $form->add('ring_type', ChoiceType::class,[
            'placeholder' => null === $ring_shape ? 'please select ring shape first' : 'now select ring type',
            'choices' => $ring_type_choices,
            'disabled' => null === $ring_shape,
            'invalid_message' => false,

        ]);
    }

这是我的树枝

代码语言:javascript
复制
{{form_start(form)}}
    {{form_row(form.ring_name)}}
    {{form_row(form.ring_shape)}}
    {{form_row(form.ring_type)}}
    {{form_row(form.size)}}
    {{form_row(form.price)}}
{{form_end(form)}}

这是控制器

代码语言:javascript
复制
 public function AddRing(Request $request)
   {  
      $ring = new Ring();
      $ring->setName('ring');
      $form = $this->createForm(RingType::class, $ring);
      $form->handleRequest($request);

      if($form->isSubmitted() && $form->isValid()){
         $ring = $form -> getData();
         $doct = $this->getDoctrine()->getManager();
         //tell doctrine you want to save the ring
         $doct->persist($ring);
         
         //execute the queries
         $doct->flush();
         return $this->redirectToRoute('custom_ring_list');
      }

      return['form' => $form->createView()];

   }
EN

回答 1

Stack Overflow用户

发布于 2022-10-24 08:44:57

我坚持要把查询

代码语言:javascript
复制
<script>
        var $ring_shape = $('#ring_ring_shape');
        $ring_shape.change(function(){
            var $form = $(this).closest('form');

            var data = {};
            data[$ring_shape.attr('name')] = $ring_shape.val();
            console.log(data);

            $.ajax({
                url: $form.attr('action'),
                type: $form.attr('method'),
                data: data,
                success: function(html){
                    $('#ring_ring_type').replaceWith(
                        $(html).find('#ring_ring_type')
                    );
                }
            });
        })
    </script>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74176641

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档