Mohammad
Mohammad

Reputation: 231

symfony entity text input in form builder?

I've two entities in symfony : first "User" entity that has a $addresses attribute that is a one to many relation to the second entity that named "UserAddresses" with fields:$id,$address(string). i want to use symfony form builder to create registration page for my website and i want a text input for the user to add his address to his account in the registration page. when i use :

$builder->add('addresses', 'entity', array(
    'class' => 'XXX\UserBundle\Entity\UserAddresses',
    'property' => 'address',
    'label' => 'label.address',
    'mapped' => false,
    'translation_domain' => 'labels'))
;

i see a input selectbox that shows the user addresses(that is already null) in the registration page and user cannot add his address to it. what should i do to replace this input element with an input type='text' element for the user to type his address

Upvotes: 2

Views: 2707

Answers (2)

ihsan
ihsan

Reputation: 2289

For User that could has many UserAddresses, use collection field type.

$builder->add('addresses', 'collection', array(
    'type' => new AddressType(),
    ...
;

For further details, see the official symfony cookbook to embed form collection and collection field type reference.

The difference with the Transformer and the collection type is that the transformer gives you only one textfield for an address and the collection type gives you a complete subform (think about address, postcode, city input fields) with the possibility to add extra addresses or delete an address.

Upvotes: 2

ZhukV
ZhukV

Reputation: 3188

You can use ModelTransformer for transform Address to field before build form, and reverse transform text value to Address object in submit form

Upvotes: 0

Related Questions