0

I would like to give formBuilder User Entity as hidden value.

$form->add('user','hidden',array("data" => $user))

$user is User Entity.

However it shows this error.

Expected argument of type "Acme\UserBundle\Entity\User", "string" given

If I use 'null' instead of 'hidden'

$form->add('user',null,array("data" => $user))

it doesn't show the error and shows the select box of user Entity.

However I would like to use hidden. How can I make it??

whitebear
  • 11,200
  • 24
  • 114
  • 237

1 Answers1

1

You did't specify the field type correctly - this is the correct way:

...
$formBuilder->add('user', HiddenType::class);
...
...
$form = $formBuilder->getForm();
$form->get('user')->setData($user->getId());

But you can't assign entity to the hidden field, so you can assign user's id for user identification.

Another option is to make data transformer and define own EntityHiddenType - more on this here: symfony : can't we have a hidden entity field?

Community
  • 1
  • 1
Jan Rydrych
  • 2,188
  • 2
  • 13
  • 18
  • I see , I understood. put Entity as hidden value is impossible. You give me the nice hint. thank you very much – whitebear Feb 06 '17 at 18:46
  • Do you want to store UserId value from user input? If not, use `$form->add('user',null,array("data" => $user, 'mapped' => false))`. – Archi Feb 06 '17 at 20:39