Bonjour,

je commence symfony 2 j'ai créer un back-office qui permet d'ajouter des soigneurs , j'ai généré les formulaires grâces au CRUD , les formulaires s'affichent correctement cependant quand je valide le formulaire rien ne s'ajoute a ma base de données et aucune erreur apparait voici le code de mon controller.

<?php

namespace MonBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use MonBundle\Entity\animaux;
use MonBundle\Form\animauxType;

/**
 * animaux controller.
 *
 * @Route("/animaux")
 */
class animauxController extends Controller
{

    /**
     * Lists all animaux entities.
     *
     * @Route("/", name="animaux")
     * @Method("GET")
     * @Template()
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('MonBundle:animaux')->findAll();

        return array(
            'entities' => $entities,
        );
    }
    /**
     * Creates a new animaux entity.
     *
     * @Route("/", name="animaux_create")
     * @Method("POST")
     * @Template("MonBundle:animaux:new.html.twig")
     */
    public function createAction(Request $request)
    {
        $entity = new animaux();
        $form = $this->createCreateForm($entity);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('animaux_show', array('id' => $entity->getId())));
        }

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }

    /**
     * Creates a form to create a animaux entity.
     *
     * @param animaux $entity The entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createCreateForm(animaux $entity)
    {
        $form = $this->createForm(new animauxType(), $entity, array(
            'action' => $this->generateUrl('animaux_create'),
            'method' => 'POST',
        ));

        $form->add('submit', 'submit', array('label' => 'Create'));

        return $form;
    }

    /**
     * Displays a form to create a new animaux entity.
     *
     * @Route("/new", name="animaux_new")
     * @Method("GET")
     * @Template()
     */
    public function newAction()
    {
        $entity = new animaux();
        $form   = $this->createCreateForm($entity);

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }

    /**
     * Finds and displays a animaux entity.
     *
     * @Route("/{id}", name="animaux_show")
     * @Method("GET")
     * @Template()
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('MonBundle:animaux')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find animaux entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),
        );
    }

    /**
     * Displays a form to edit an existing animaux entity.
     *
     * @Route("/{id}/edit", name="animaux_edit")
     * @Method("GET")
     * @Template()
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('MonBundle:animaux')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find animaux entity.');
        }

        $editForm = $this->createEditForm($entity);
        $deleteForm = $this->createDeleteForm($id);

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        );
    }

    /**
    * Creates a form to edit a animaux entity.
    *
    * @param animaux $entity The entity
    *
    * @return \Symfony\Component\Form\Form The form
    */
    private function createEditForm(animaux $entity)
    {
        $form = $this->createForm(new animauxType(), $entity, array(
            'action' => $this->generateUrl('animaux_update', array('id' => $entity->getId())),
            'method' => 'PUT',
        ));

        $form->add('submit', 'submit', array('label' => 'Update'));

        return $form;
    }
    /**
     * Edits an existing animaux entity.
     *
     * @Route("/{id}", name="animaux_update")
     * @Method("PUT")
     * @Template("MonBundle:animaux:edit.html.twig")
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('MonBundle:animaux')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find animaux entity.');
        }

        $deleteForm = $this->createDeleteForm($id);
        $editForm = $this->createEditForm($entity);
        $editForm->handleRequest($request);

        if ($editForm->isValid()) {
            $em->flush();

            return $this->redirect($this->generateUrl('animaux_edit', array('id' => $id)));
        }

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        );
    }
    /**
     * Deletes a animaux entity.
     *
     * @Route("/{id}", name="animaux_delete")
     * @Method("DELETE")
     */
    public function deleteAction(Request $request, $id)
    {
        $form = $this->createDeleteForm($id);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $entity = $em->getRepository('MonBundle:animaux')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find animaux entity.');
            }

            $em->remove($entity);
            $em->flush();
        }

        return $this->redirect($this->generateUrl('animaux'));
    }

    /**
     * Creates a form to delete a animaux entity by id.
     *
     * @param mixed $id The entity id
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm($id)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('animaux_delete', array('id' => $id)))
            ->setMethod('DELETE')
            ->add('submit', 'submit', array('label' => 'Delete'))
            ->getForm()
        ;
    }
}

Merci d'avance

2 réponses


Ce serait sympas de donner la méthode qui pose problème...

Est ce qu'on pourrait voir ton fichier twig qui permet l'affichage du formulaire, peut être que tu as oublié de déclarer le champ caché.