Search code examples
phpsymfonysymfony4

Get certain form inputs separately


I have this form and I have to get the values of 3 fields (name and 2 dates) separately, formate the dates and put them into an arrayCollection. I only want to get these 3 fields and let the rest of the form field get inserted automatically as usual. In parallel, when I come back to edit the form, I want to know how to distribute these values to populate the form.


Solution

  • According to this documentation creating-form-classes , an attempt of solution is to do something like :

    We assume that your have an entity name Article.

    • Your form can be something like :
        class ArticleType extends AbstractType
        {
            public function buildForm(FormBuilderInterface $builder, array $options)
            {
                $builder->add('title', TextType::class)
                        ->add('author', TextType::class)
                        ->add('name', TextType::class, [
                            'mapped' => false
                        ])
                        ->add('date1', DatetimeType::class, [
                            'mapped' => false
                        ])
                        ->add('date2', DatetimeType::class, [
                            'mapped' => false
                        ]);
            }
    
            public function configureOptions(OptionsResolver $resolver)
            {
                $resolver->setDefaults([
                    'data_class' => Article::class,
                ]);
            }
        }
    
    

    Here there is option 'mapped' => false because you don't want to mappe it with the entity Article.

    • Then in your controller, you can have something like
    
        /**
         * @Route("/articles", methods={"POST", "GET"}, name="app_post_article")
         */
        public function postArticle(Request $request, EntityManagerInterface $em)
        {
            $form = $this->createForm(ArticleType::class);
            $form->handleRequest($request);
    
            if ($request->isMethod('POST') && $form->isSubmitted() && $form->isValid()) {
                $name = $form->get('name')->getData();
                $date1 = $form->get('date1')->getData();
                $date2 = $form->get('date2')->getData();
                //.... do something
            }
            //... Do other thing
    
        }
    
        /**
         * @Route("/articles/{id}", methods={"POST", "GET"}, name="app_edit_article")
         */
        public function editArticle(Request $request, Article $article, EntityManagerInterface $em)
        {
            $form = $this->createForm(ArticleType::class, $article);
            // $prevName, $prevDate1, $prevDate2 must be retreive first...
            $form->get('name')->setData($prevName);
            $form->get('date1')->setData($prevDate1);
            $form->get('date2')->setData($prevDate2);
            $form->handleRequest($request);
    
            if ($request->isMethod('POST') && $form->isSubmitted() && $form->isValid()) {
                $name = $form->get('name')->getData();
                $date1 = $form->get('date1')->getData();
                $date2 = $form->get('date2')->getData();
                //.... do something
            }
            //... Do other thing
    
        }
    
    

    It's just an idea.