Symfony $form->getData() return empty Object with values concatenated

623 views Asked by At

This is my controller

/**
 * @Route("/offre/add", name="offre_add")
 * @param Request $request
 * @return RedirectResponse|Response
 * @throws \Exception
 */
public function addOffre(Request $request) {
    $offre = new Offre();

    $em = $this->getDoctrine()->getManager();
    $formOffre = $this->createForm(OffreType::class, $offre);

    $formOffre->handleRequest($request);

    if ($formOffre->isSubmitted() && $formOffre->isValid()) {
        $offre = $formOffre->getData();
        dd($offre);
    }

    return $this->render('admin/offre/add_offre.html.twig', [
        'form' => $formOffre->createView()
    ]);
}

And this is my OffreType

<?php

namespace App\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class OffreType extends AbstractType {
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
            ->add('numeroOffre', TextType::class)
            ->add('version', TextType::class)
            ->add('statut', TextType::class)
            ->add('signature', TextType::class)
            ->add('prixTotal', TextType::class)
            ->add('commentaire', TextareaType::class)
            ->add('visibilite', ChoiceType::class, array('choices' => array(
                'Oui' => 'Oui',
                'Non' => 'Non')
            ))
            ->add('save', SubmitType::class)
        ;
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'App\Entity\Offre'
        ));
    }
}

When I try to validate my form like this :

enter image description here

The result is an empty Object, and the values I inserted in my form are concatenated to my object (with the same Key/Value)

enter image description here

If I print the $_POST variable, the form seems to works fine

enter image description here

Why is the method getData() not working ?

edit:

This is my Offre Class

<?php

namespace App\Entity;

use DateTime;
use Doctrine\ORM\Mapping as ORM;

/**
 * Offre
 *
 * @ORM\Table(name="offre")
 * @ORM\Entity(repositoryClass="App\Repository\OffreRepository")
 */
class Offre
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="numero_offre", type="string", length=50)
     */
    private $numeroOffre;

    /**
     * @var string
     *
     * @ORM\Column(name="statut", type="string", length=255, nullable=true)
     */
    private $statut;

    /**
     * @var string
     *
     * @ORM\Column(name="version", type="string", length=255, nullable=true)
     */
    private $version;

    /**
     * @var string
     *
     * @ORM\Column(name="signature", type="string", length=255, nullable=true)
     */
    private $signature;

    /**
     * @var string
     *
     * @ORM\Column(name="commentaire", type="string", length=255, nullable=true)
     */
    private $commentaire;

    /**
     * @var string
     *
     * @ORM\Column(name="visibilite", type="string", length=255)
     */
    private $visibilite;

    /**
     * @var string
     *
     * @ORM\Column(name="prix_total", type="string", length=255)
     */
    private $prixTotal;

    /**
     * @var DateTime
     *
     * @ORM\Column(name="date_create", type="date", nullable=true)
     */
    private $dateCreate;

    /**
     * @var string
     *
     * @ORM\Column(name="user_create", type="string", length=255)
     */
    private $userCreate;

    /**
     * @var DateTime
     *
     * @ORM\Column(name="date_change", type="date", nullable=true)
     */
    private $dateChange;

    /**
     * @var string
     *
     * @ORM\Column(name="user_change", type="string", length=255, nullable=true)
     */
    private $userChange;

    /**
     * @var boolean
     *
     * @ORM\Column(name="active", type="boolean")
     */
    private $active = true;

    /**
     * @ORM\ManyToOne(targetEntity=Client::class, inversedBy="offres")
     */
    private $client;


    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param int $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return string
     */
    public function getNumeroOffre()
    {
        return $this->numeroOffre;
    }

    /**
     * @param string $numeroOffre
     */
    public function setNumeroOffre($numeroOffre)
    {
        $this->$numeroOffre = $numeroOffre;
    }

    /**
     * @return string
     */
    public function getPrixTotal()
    {
        return $this->prixTotal;
    }

    /**
     * @param string $prixTotal
     */
    public function setPrixTotal($prixTotal)
    {
        $this->$prixTotal = $prixTotal;
    }

    /**
     * @return string
     */
    public function getStatut()
    {
        return $this->statut;
    }

    /**
     * @param string $statut
     */
    public function setStatut($statut)
    {
        $this->$statut = $statut;
    }

    /**
     * @return string
     */
    public function getVersion()
    {
        return $this->version;
    }

    /**
     * @param string $version
     */
    public function setVersion($version)
    {
        $this->$version = $version;
    }

    /**
     * @return string
     */
    public function getSignature()
    {
        return $this->signature;
    }

    /**
     * @param string $signature
     */
    public function setSignature($signature)
    {
        $this->$signature = $signature;
    }

    /**
     * @return string
     */
    public function getCommentaire()
    {
        return $this->commentaire;
    }

    /**
     * @param string $commentaire
     */
    public function setCommentaire($commentaire)
    {
        $this->$commentaire = $commentaire;
    }

    /**
     * @return string
     */
    public function getVisibilite()
    {
        return $this->visibilite;
    }

    /**
     * @param string $visibilite
     */
    public function setVisibilite($visibilite)
    {
        $this->$visibilite = $visibilite;
    }

    /**
     * @return DateTime
     */
    public function getDateCreate()
    {
        return $this->dateCreate;
    }

    /**
     * @param DateTime $dateCreate
     */
    public function setDateCreate($dateCreate)
    {
        $this->dateCreate = $dateCreate;
    }

    /**
     * @return string
     */
    public function getUserCreate()
    {
        return $this->userCreate;
    }

    /**
     * @param string $userCreate
     */
    public function setUserCreate($userCreate)
    {
        $this->userCreate = $userCreate;
    }

    /**
     * @return DateTime
     */
    public function getDateChange()
    {
        return $this->dateChange;
    }

    /**
     * @param DateTime $dateChange
     */
    public function setDateChange($dateChange)
    {
        $this->dateChange = $dateChange;
    }

    /**
     * @return string
     */
    public function getUserChange()
    {
        return $this->userChange;
    }

    /**
     * @param string $userChange
     */
    public function setUserChange(string $userChange)
    {
        $this->userChange = $userChange;
    }

    /**
     * @return bool
     */
    public function isActive()
    {
        return $this->active;
    }

    /**
     * @param bool $active
     */
    public function setActive(bool $active)
    {
        $this->active = $active;
    }

    public function getClient()
    {
        return $this->client;
    }

    public function setClient(?Client $client)
    {
        $this->client = $client;

        return $this;
    }
}
1

There are 1 answers

0
Skipper NB On BEST ANSWER

The problem comes from dollar "$" symbol in the setters entity definition.

 /**
 * @param string $prixTotal
 */
public function setPrixTotal($prixTotal)
{
    $this->$prixTotal = $prixTotal;
}

Have just delete them and it works well.