Bonsoir à tous, je vient de trouver la fonction DateType de Symfony mais quand je valide avec une date j'ai cette erreur.

Expected argument of type "string or null", "object" given at property path "date".

Voici mon contrôleur

<?php
// src/Controller/FormController.php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use App\Form\LibelleFormType;
use App\Entity\Libelle;

class LibelleController extends AbstractController
{
    /**
 * @Route("/libelle")
 */
public function new(Request $request)
{
    $libelle = new Libelle();
    $libelle->setNom('Hello World');

    $form = $this->createForm(LibelleFormType::class, $libelle);

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();

        $formValues = $request->request->get('libelle_form');
        $nom = $formValues['nom'] ?? null;

        if( $libelle->getNom() == null)
{
$libelle->setValidation('vide');
}

        $libelle->setAuteur($nom);

        $em->persist($libelle);
        $em->flush();
    }

    return $this->render('default/new.html.twig', array(
        'form' => $form->createView(),
    ));
}
}

Voici mon formtype

<?php

namespace App\Form;

use App\Entity\Libelle;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\DateType;
class LibelleFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('nom')
            ->add('date')
            ->add('date', DateType::class, array(
                'widget' => 'single_text',
                'format' => 'yyyy-MM-dd',
                'required' => false
                ))
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Libelle::class,
        ]);
    }
}

Et voici mon entity

<?php

namespace App\Entity;

use App\Repository\LibelleRepository;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=LibelleRepository::class)
 */
class Libelle
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

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

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

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

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

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

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

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

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

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

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

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

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getNom(): ?string
    {
        return $this->nom;
    }

    public function setNom(?string $nom): self
    {
        $this->nom = $nom;

        return $this;
    }

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

    public function setClient(?string $client): self
    {
        $this->client = $client;

        return $this;
    }

    public function getDate(): ?string
    {
        return $this->date;
    }

    public function setDate(?string $date): self
    {
        $this->date = $date;

        return $this;
    }

    public function getTemps(): ?string
    {
        return $this->temps;
    }

    public function setTemps(?string $temps): self
    {
        $this->temps = $temps;

        return $this;
    }

    public function getNbroperation(): ?string
    {
        return $this->nbroperation;
    }

    public function setNbroperation(?string $nbroperation): self
    {
        $this->nbroperation = $nbroperation;

        return $this;
    }

    public function getNbrcomptedebit(): ?string
    {
        return $this->nbrcomptedebit;
    }

    public function setNbrcomptedebit(?string $nbrcomptedebit): self
    {
        $this->nbrcomptedebit = $nbrcomptedebit;

        return $this;
    }

    public function getValidation(): ?string
    {
        return $this->validation;
    }

    public function setValidation(?string $validation): self
    {
        $this->validation = $validation;

        return $this;
    }

    public function getAuteur(): ?string
    {
        return $this->auteur;
    }

    public function setAuteur(?string $auteur): self
    {
        $this->auteur = $auteur;

        return $this;
    }

    public function getDiffere(): ?string
    {
        return $this->differe;
    }

    public function setDiffere(?string $differe): self
    {
        $this->differe = $differe;

        return $this;
    }

    public function getDateoperation(): ?string
    {
        return $this->dateoperation;
    }

    public function setDateoperation(?string $dateoperation): self
    {
        $this->dateoperation = $dateoperation;

        return $this;
    }

    public function getDateid(): ?string
    {
        return $this->dateid;
    }

    public function setDateid(?string $dateid): self
    {
        $this->dateid = $dateid;

        return $this;
    }
}

Cordialement.

3 réponses


Bonsoir,

tout simplement car dans ton entity les champs date doivent avoir le type Datetime et non string

J'ai modifier l'entity avec date ansi que la base de donnée, mais j'ai la meme erreur.

Il faut aussi changer le typage des fonctions:

public function getDate(): ?string
    {
        return $this->date;
    }

    public function setDate(?string $date): self
    {
        $this->date = $date;

        return $this;
    }