Symfony dynamic EntityType data attribute

  forms, symfony

I have a simple symfony form and i use this form for create and edit an object. When i try to create the form there is a user should be always selectd when someone creating and when editing it should be the selected user (if the author wanted to change the default user from the list).

something like this:

->add('user', EntityType::class, [
  'class' => User::class,
  'label' => 'user',
  'choice_label' => 'name',
  'data' => $this->em->getRepository(User::class)->find(1),
])

Now it works on create but when i change the value and then try to edit after save it gives me the default User agean.

so is there is any way to make data dynamic when the form is new? by the way i have an option in my form tells when the form new (‘new’ => true).

Source: Symfony Questions

LEAVE A COMMENT