Reputation: 2091
I have a form which I create using a form class. One field of this form is country_id, which gives me a country id in a text field.
However, I want create a select field instead, and not only with all ids, but rather with country names mapped by those ids.
I have a table in a database mapping country ids to names. However, the form uses only one entity, not this one.
How can I create that select using this other table here? How can I solve this?
Thanks!
EDIT:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', 'text', ['required' => false])
->add('address', 'text', ['required' => false])
->add('zipCode', 'text', ['required' => false])
->add('city', 'text', ['required' => false]);
$builder->add('country', 'entity', [
'class' => 'AcmeBundle:Country',
'property' => 'shortName',
'required' => false,
]);
// $builder->add('country', 'text', ['required' => false]);
$builder->add('number', 'text', ['required' => false]);
}
That's the whole method inside the, let's call it, Foo entity. It generates proper HTML with ids as values, but there are still those errors I mentioned in the comment.
Upvotes: 2
Views: 210
Reputation: 6410
You need the entity Field Type
. Your FormType
should look like this:
$builder->add('country', 'entity', array(
'class' => 'YourBundle:Country',
'property' => 'name',
));
Checkout the docs for more info.
Upvotes: 2