0

I have this field in my form:

    ->add('taskOwner', null, array(
        'label' => $this-> translator ->trans( 'tasks.index.responsible' , array() , 'crm' )))

Symfony recognize it as Choice Type (it's have foreign key to another table, with users). Now i want to set the default value on the logged user. How I can do that ? I tried in my controller create new entity of my type, set taskOwner into it and then by SetData put in into form, like this:

    $entity = new Tasks();
    $tasksForm = $this->createForm(new TasksType($translator), $entity);

    $userId = $this->get('security.context')->getToken()->getUser()->getId();
    $user = $this->getDoctrine()->getRepository('CloudAdmBundle:AdmUser')->find($userId);
    $task = new Tasks();
    $task->setTaskOwner($user);
    $tasksForm->setData($task);

To clear everything, definition of setter:

   public function setTaskOwner(\Cloud\AdmBundle\Entity\AdmUser $taskOwner = null)
    {
        $this->taskOwner = $taskOwner;

        return $this;
    }

1 Answer 1

1

Do it before you create the form:

$userId = $this->get('security.context')->getToken()->getUser()->getId();
$user = $this->getDoctrine()->getRepository('CloudAdmBundle:AdmUser')->find($userId);
$entity = new Tasks();
$entity->setTaskOwner($user);
$tasksForm = $this->createForm(new TasksType($translator), $entity);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.