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.
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.
Related
I have a form containing two EntityType fields, first one is category and the second is subCategory, the category field will have all the categories that i have in the database, but i want to add one more to these, which is "all categories", and i want it to be the one selected by default, how can i achieve this..
$builder
->add(
'category',
EntityType::class,
[
'label' => 'Catégorie',
'class' => Category::class,
'choice_label' => 'title',
'mapped' => false
]
)
->add(
'subCategory',
EntityType::class,
[
'label' => 'Sous-catégorie',
'class' => SubCategory::class,
'choice_label' => 'title',
'mapped' => false
]
);
You can construct your array of choices manually and use ChoiceType, for example:
// retrieve list of your categories in your controller or somewhere else
// and pass it using the options. Add it also before the default option "All categories".
$categories = $options['categories'];
$builder->add('category', ChoiceType::class, [
'choices' => $categories,
...
Using Symfony 3 with FOSUserBundle, how do I make a custom field of my user registration read only)
In RegistrationFormType.php, buildForm method I tried:
->add('name', 'text', array('read_only' => true))
which gets InvalidArgumentException
->add('name', 'text', array('read_only' => true))
which gets UndefinedOptionsException
Try to do this :
->add('name', TextareaType::class, array(
'attr' => array(
'readonly' => true,
),
));
I have two properties with the same name from different tables declared in my GameType.php file.
But these are actually not the same values as the table columns Game.name and Type.name come from different tables.
How can I display both of them in the form without conflicts?
Snippet from GameType.php:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add( 'name', TextType::class, [
'class' => 'AppBundle:Game',
'choice_label' => 'name',
] );
$builder
->add( 'name', EntityType::class, [
'class' => 'AppBundle:Type',
'choice_label' => 'name',
'multiple' => false,
'expanded' => false
] );
}
They're both called 'name' which causes the Type's selectbox to override the Game's TextField.
I call the widget, but that's not working because they are both called 'name':
{{ form_widget(form.name) }}
Why don't you change the properties name in the entity ? you can label them the same in the view after that if you need .
I'm using form to edit some data.
one of the form field:
$builder ->add('cities', 'entity', array(
'class' => 'Vendor\SomeBundle\Entity\City',
'multiple' => true
));
everthing works fine - i have some preselected cities in accordance with the database.
Now i want to limit choices to cities form specific country.
So I get collection of cities and set 'choices' option:
$builder ->add('cities', 'entity', array(
'class' => 'Vendor\SomeBundle\Entity\City',
'multiple' => true,
'choices' => $citiesCollection
));
The choice list is limited but no cities are selected.
i try to set this preselected cities using 'data' option but this also does not work
$builder ->add('cities', 'entity', array(
'class' => 'Vendor\SomeBundle\Entity\City',
'multiple' => true,
'choices' => $citiesCollection
'data' => $citiesSelected
));
trying different approaches, passing ArrayCollection, array, array of keys but nothing works...
It is even possible?
The working solution - set 'query_builder" option instead of 'choices'
$builder ->add('cities', 'entity', array(
'class' => 'Vendor\SomeBundle\Entity\City',
'multiple' => true,
'query_builder' => $citiesQueryBuilder
));
now rendered form has selected cities
I think that assigning the $citiesSelected array to the cities field of the base entity assigned to the form will make the trick.
I have a Blog entity which is supposed to have one Poll entity and each Poll entity has many Choice entities associated to it. I was trying to create an "Add/Edit Poll" form for the blog. In that form, in addition to the Poll form fields I want to have a field that allows you to add Choices (a simple text field each). I'm having trouble figuring out how I should code this so that it works well. The PollType form looks like this:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('title', 'text');
$builder->add('choices', 'collection',
array(
'label_attr' => array(
'style' => 'vertical-align: top;'
),
'type' => 'text',
'required' => false,
'allow_add' => true,
'allow_delete' => true,
'delete_empty' => true,
'attr' => array(
'style' => 'display: inline-block; margin: 0 0 20px 20px; width: 200px;'
)));
$builder->add('description', 'textarea', array('attr' => array('rows' => 6)));
$builder->add('save', 'submit', array('label' => 'Create Poll'));
$builder->getForm();
}
Originally I had 'type' => new ChoiceType() with ChoiceType being:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('title', 'text');
}
But I had to remove all this because "Variable "expanded" does not exist in bootstrap_3_layout.html.twig" error that I could not find any solution for. I changed the type to text but now I'm stumped as to how I would collect the data from each "choices" field, persist it to the Choices entity, and also associating all of them with the new Poll (which may not have been inserted yet so I don't have an ID to add to Choices->pollID). Is there a standard way for doing this kind of thing that I'm missing?
If you have Poll entity and Choice entity, so it looks like this:
$builder->add('choice', 'entity', array(
'class' => 'AcmeHelloBundle:Choice',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('c')
->orderBy('choice.name', 'ASC');
},
));
Also you must have OneToMany or ManyToMany association with your entities.
More about entity Field Type