简体   繁体   中英

form_widget submit doesn't work

I'm trying to customize my form created with formBuilder.

My form is working well when i display it with {{ form(comment) }} but doesn't work anymore with {{ form_widget(comment.MYMETHOD) }}

I display my form like this :

<form class="commentForm">
    <ul>
        <li class="commentFormArea">{{ form_widget(comment.commentaires) }}
        </li>
        <li class="commentFormsubmit">{{ form_widget(comment.save) }}</li>
    </ul>
</form>

My controller

 public function postsAction(Request $request)
{
    $repository = $this
        ->getDoctrine()
        ->getManager()
        ->getRepository('NastycodeFrontBundle:Publication')
    ;
    $posts = $repository->findBy(array(), array(), 10);

    $commentaires = new Commentaires();

    $comment = $this->get('form.factory')->createBuilder('form', $commentaires)
        ->add('commentaires', 'textarea')
        ->add('save',      'submit')
        ->getForm()
    ;

    // On fait le lien Requête <-> Formulaire
    // À partir de maintenant, la variable $commentaires contient les valeurs entrées dans le formulaire par le visiteur
    $comment->handleRequest($request);

    // On vérifie que les valeurs entrées sont correctes
    // (Nous verrons la validation des objets en détail dans le prochain chapitre)
    if ($comment->isValid()) {
        // On l'enregistre notre objet $commentaires dans la base de données, par exemple
        $em = $this->getDoctrine()->getManager();
        $em->persist($commentaires);
        $em->flush();

        $request->getSession()->getFlashBag()->add('notice', 'Annonce bien enregistrée.');

        // On redirige vers la page de visualisation de l'annonce nouvellement créée
        return $this->redirect($this->generateUrl('nastycode_comment_code', array('id' => $commentaires->getId())));
    }

    $user = $this->getUser();
    return $this->render('NastycodeFrontBundle:Posts:posts.html.twig', array(
        'user' => $user,
        'posts' => $posts,
        'comment' => $comment->createView(),
    ));
}

When i submit my form, it generate this url

/web/app_dev.php/nastycodes?form[commentaires]=MYCOMMENT&form[save]=

Instead of this

/web/app_dev.php/nastycodes?id=1

I have no idea why my submit doesn't work when i don't display all the form.

Do you guys know what is the problem and how i can fix it ?

Thanks

Not sure which version of Symfony2 are you using but since v2.3 there are form_start() and form_end() Twig functions which could be used to print form open and form close tags, including its attributes.

You would, however, need to do this:

$comment = $this->get('form.factory')->createBuilder('form', $commentaires)
        ->setMethod("POST") // <--- THIS
        ->add('commentaires', 'textarea')
        ->add('save',      'submit')
        ->getForm()
    ;

And then:

{{ form_start(comment) }}
    <ul>
        <li class="commentFormArea">{{ form_widget(comment.commentaires) }}
        </li>
        <li class="commentFormsubmit">{{ form_widget(comment.save) }}</li>
    </ul>
{{ form_end(comment) }}

By default, form_end automatically invokes form_rest(form) ...

Try to add method="post" to the <form> tag, the default method of form is GET.

When you use {{ form(comment) }} it generates all fields of the form in the template. Try to add form_rest(comment) before form closing tag </form> ( documentation ) to ensure you didn't omit something.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM