Can I put html inside a Symfony form button with Twig?

前端 未结 5 1674
暗喜
暗喜 2021-02-02 09:06

I\'m trying to put html inside a form button with twig like:

{{ form_widget(form.jiraStatus, {
        \'label\': \'Bug\',         


        
5条回答
  •  不要未来只要你来
    2021-02-02 09:58

    this is how I solved and tested with Symfony 4. In twig template:

    {{form_start(form)}}
    {{form_widget(form)}}
    
    {{form_end(form)}}

    In my PHP controller form I did not add any button but just input fields.

       $form = $this->createFormBuilder($article)
            ->add('title',TextType::class, array(
                'data' => $article->getTitle(),
                'attr' => array('class' => 'form-control')))
            ->add('body', TextareaType::class, array(
                'data' => $article->getBody(),
                'required' => false,
                'attr' => array('class' => 'form-control')))
            ->getForm();
    

    What I did to check the form submission is:

    if($form->isSubmitted() ){
        if($request->request->get('create') && $form->isValid()){
            $article = $form->getData();
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($article);
            $entityManager->flush();
        } //l'alternativa può solo essere il cancel
        return $this->redirectToRoute('article_list');    
    }
    

    Hope this can help. Good to say that even the problem of buttons alignment is solved because div is not added for every button as form add method does.

提交回复
热议问题