user3720172
user3720172

Reputation: 39

Entity Field Type symfony2

I have a Dropdown Field (City), I add this filed into UserType

$builder->add('name', 'text', array('label' => 'form.name', 'translation_domain' => 'FOSUserBundle'));
$builder->add('phone', 'text', array('label' => 'form.phone', 'translation_domain' => 'FOSUserBundle'));
$builder->add('cityId', 'entity', array(
    'label' => 'form.city', 
    'translation_domain' => 'FOSUserBundle',
    'class' => 'AppBundle:City',
    // 'choice_label' => 'name',
    // 'choice_value' => 'id',
));

But when I submit to form the cityId field sends me the City Name not the ID. How can I fix it ?

Upvotes: 0

Views: 35

Answers (1)

rommct
rommct

Reputation: 228

I think property field can help you :) In form you should add property => name

$builder->add('cityId', 'entity', array(
    'label' => 'form.city', 
    'translation_domain' => 'FOSUserBundle',
    'class' => 'AppBundle:City',
    'property' => 'name',
));

Or in your entity write toString function like

$builder->add('cityId', 'entity', array(
    'label' => 'form.city', 
    'translation_domain' => 'FOSUserBundle',
    'class' => 'AppBundle:City',
    // toString() function called
));

City.php

public function __toString()
{
    return $this->name;
}

Upvotes: 1

Related Questions