Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8395131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:13:17+00:00 2026-06-09T20:13:17+00:00

I have a form type in symfony2 : namespace Acme\SomethingBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilder;

  • 0

I have a form type in symfony2 :

namespace Acme\SomethingBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class GuestType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('email')
            ->add('address')
            ->add('phone')
            ->add('created_at')
            ->add('updated_at')
            ->add('is_activated')
            ->add('user','entity', array('class'=>'Acme\SomethingBundle\Entity\User', 'property'=>'id' ));
    }

    public function getName()
    {
        return 'acme_somethingbundle_guesttype';
    }
}

The action:

    /**
     * Displays a form to create a new Guest entity.
     *
     */
    public function newAction()
    {
        $entity = new Guest();
        $form   = $this->createForm(new GuestType(), $entity);

        return $this->render('AcmeSomethingBundle:Guest:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView()
        ));
    }

    /**
     * Creates a new Guest entity.
     *
     */
    public function createAction()
    {
        $entity  = new Guest();
        $request = $this->getRequest();
        $form    = $this->createForm(new GuestType(), $entity);
        $form->bindRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('guest_show', array('id' => $entity->getId())));

        }

        return $this->render('AcmeSomethingBundleBundle:Guest:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView()
        ));
    }

Twig template

<h1>Guest creation</h1>
<form action="{{ path('guest_create') }}" method="post" {{ form_enctype(form) }}>
    {{ form_widget(form) }}
    <p>
        <button type="submit">Create</button>
    </p>
</form>

<ul class="record_actions">
    <li>
        <a href="{{ path('guest') }}">
            Back to the list
        </a>
    </li>
</ul>

the ‘user’ property takes an entity of user Ids as a variable and in the twig form is displayed as a drop down box. I want the current logged in (authenticated) user’s Id to insert automatically – and hidden If possible – in the ‘user’ field. I know that I am getting a user’s Id with

$user = $this->get('security.context')->getToken()->getUser();
$userId = $user->getId();

but I cannot make it work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:13:19+00:00Added an answer on June 9, 2026 at 8:13 pm

    You have to give the set the user in your Guest object before injecting it in the form if you want to have it in the form, and editable by the user:

    $entity  = new Guest();
    $entity->setUser($this->get('security.context')->getToken()->getUser());
    $form    = $this->createForm(new GuestType(), $entity);
    

    Otherwise, if it’s not editable, you should remove this field from the form and set user after the isValid() test.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have want to save a form in Symfony2, based on a form type,
We can easily custom the powerful Symfony2 form component with an entity Field Type
I have an entity type form field in my Symfony2 project. $builder = $this->createFormBuilder();
i do use symfony2.1 and i have form which i do extend by another
I have a custom form type for a Rating entity in my Symfony2 project.
I have some *Type form classes and a forms.html.twig to customize form appearance. By
I have a Tabular type Form based upon a SELECT * FROM table type
I have a form with number of submit type as images. Each image has
I have a form which POST's its data via AJAX: $.ajax({ type: POST, url:
I have a form which allow users to choose the type of permit they

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.