ramiromd
ramiromd

Reputation: 2029

Collection form and user

im developing a simple user profile editor for my app. The values that user can edit are, e-mail, password and phone numbers.

Obviouslly, i have the User entity, that have an Phone entity collection. Also, i have the UserProfileType and the PhoneType.

The buildForm method of userProfileType, include the phone numbers:

$builder 
    ->add('telefonos','collection', array(
       'type' => new TelefonoType(),
       'required' => false,
       'allow_add' => true,
       'allow_delete' => true
    ))

And the PhoneType buildForm is:

$builder
    ->add('valor','text',array(
        'required' => false
    ))
    ->add('descripcion','text', array(
        'required' => false
    ))
    ->add('user')
;

The phone numbers are displayed correctly in Twig template...but the user field is displayed as combobox and allow the user "change the user value". I wish that the user value will be implicit. How can do this ?.

I am newest with Symfony forms. Please, help me. Thanks.

Upvotes: 0

Views: 40

Answers (1)

maphe
maphe

Reputation: 1931

As you didn't tell Symfony what type should be your user field, it's trying to guess what is the most appropriate type (you can look here for some details). So, in your Telephone entity, I suppose that the user attribute is declared as a User (another entity of yours).

As a consequence, Symfony create a form field with entity type (doc here).

If you want an "implicit user set", this is quite impossible, you have to remove the field addition in you form builder, and call setUser manualy on your entity(ies) in your controller action or a service.

You can get the current logged in user from the security token, or directly in a controller action : $this->getUser()

Upvotes: 1

Related Questions