Juan I. Morales Pestana
Juan I. Morales Pestana

Reputation: 1147

symfony form invalid datetime field sended by api

I'm working in an api (Angular/Symfony) and I tried to submit my form but the form always says this value is not valid for the date or datetimes field of the entity. even when I send back the same date with out changes.

Action

/**
     * @Rest\Route("/editar/{slug}")
     * @Rest\View()
     */
    public function editarEmbarque(Request $request, $slug)
    {
        $embarque = $this->getDoctrine()->getRepository('AppBundle:Embarque')->findOneBySlug(trim($slug));
        $response = [];

        if ($embarque) {
            $body = $request->getContent();
            $data = json_decode($body, true);

            $form = $this->createForm('AppBundle\Form\EmbarqueType', $embarque);
            $form->submit($data);

            if ($form->isValid()) {
                $em = $this->get('doctrine.orm.entity_manager');

                $em->persist($embarque);
                $em->flush();
                return ['status' => 'si'];
            }

            return ['status' => 'no'];
        }
        return ['status' => '404'];
    }

the entity has many datetime field as the form.

this is the response when I do a dump to the error origin and the message

string(20) "entregaFacturacionAt"
string(24) "This value is not valid."
{"status":"no"}

also the data has no problem

array(11) {
  ["bl"]=>
  string(5) "mi bl"
  ["manifiesto"]=>
  string(24) "mi otro manifiesto kkk"
  ["facturaProveedor"]=>
  array(0) {
  }
  ["embarqueContenedores"]=>
  array(0) {
  }
  ["habilitadoMariel"]=>
  bool(false)
  ["liberadoBlHouse"]=>
  bool(false)
  ["liberadoBlMaster"]=>
  bool(false)
  ["slug"]=>
  string(30) "mi-bl-mi-otro-manifiesto-kkk"
  ["createdAt"]=>
  string(25) "2018-03-22T08:27:37-04:00"
  ["updatedAt"]=>
  string(25) "2018-03-22T17:41:11-04:00" //this is a date unchanged
  ["entregaFacturacionAt"]=>
  string(10) "2018-03-24" // this is a date changed
}

I'm forgetting something, I used datatransformers but no results. any idea?

thanks in advance.

EDIT: FORM EXAMPLE

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('regular_no_date_fields') // <= it could be text,string,number,array... it works
            ->add('blAt', DateType::class);                                             

    }

EDIT: POSTED DATA

{
    "bl":"mi bl",
    "blAt": "Fri Mar 30 2018", <= this is one of the formats, i tried also with m-d-y y-m-d or changing - for /
    "manifiesto":"mi manifiesto",
    "pais":"China",
    "puertoOrigen":"Tsing ao",
    "puertoDestino": "Mariel",
    "embarqueContenedores":[]
}

the interesting part is that when I set the data directly to the entity and use de validator all works fine. For example the post data above worked fine.

Upvotes: 0

Views: 1745

Answers (2)

Houssein Zouari
Houssein Zouari

Reputation: 722

I found this issue when i was working on AngularJs and Symfony . You should send you date type like this.

{"date":"2018-03-22T23:00:00.000Z"}

And from the backend :

$builder->add('date', 'datetime', ['widget' => 'single_text']);

Upvotes: 2

Chris
Chris

Reputation: 799

Try declaring your DateType like this:

$builder
  ->add('yourDateField', DateType::class, array(
            'input' => 'string', //depends on format of underlying object
            'widget' => 'single_text'
        ))

Upvotes: 3

Related Questions