Droyo
Droyo

Reputation: 23

Getting specific Data from a form symfony2

My problem is that I want to set a value to one field of an Entity and this value comes from a form.

The form is bound to another entity and this field of the form is named fmedida.

This is what I've tried

$hijo-> setFinicio(new \DateTime($form->getData()->getfmedida()));

But of course the syntax is not correct since I have this error message:

"DateTime::__construct() expects parameter 1 to be string, object given"

Upvotes: 0

Views: 559

Answers (1)

Faery
Faery

Reputation: 4650

I advise you to var_dump the value that you get from $form->getData()->getfmedida() and see what is happening.

When I tried getData(), it returns array on my side, so also try:

$data = $form->getData();
$fmedida = $data['fmedida'];

$hijo-> setFinicio(new \DateTime($fmedida));

Also, DateTime creates DateTime objects from strings. Obviously, the value which you are getting from $form->getData()->getfmedida() is not a string, it's a object. If $form->getData()->getfmedida() gives you a DateTime object, I can't see a point in using DateTime, so just

$hijo-> setFinicio($form->getData()->getfmedida());

should be enough.

Upvotes: 2

Related Questions