I'm working on a form and want to pass to my form an array that would content all my projects so that my form would have a select with all of those project for option.
I already reed alot of answer, but I just can't figure this thing out. I know that I'm suppose to do something like:
$formulaire=$this->createForm(new ModifierSupprimerProjet(), null, $myArray);
but, I'm suppose to add all the content of my array to getDefaultOptions()... I do I do that? An other thing, what is suppose to be the second parameters of the createForm method?
Here is the post that that came the closest to solve my probleme:
The best way to populate select form element with Project entities is to use entity form field. So you don't have to pass any options to form type class.
$builder
->add('project', 'entity', [
'label' => 'form.project',
'class' => 'AppBundle\Entity\Project',
'property' => 'name',
'required' => true,
'empty_value' => 'form.select_empty_value',
'query_builder' => function($repository) {
return $repository->createQueryBuilder('p')->add('orderBy', 'p.name ASC');
},
])
;
If you need to populate choice field there is another best practice. You should define your form type class as service and inject Entity Manager into it. So you could fetch any data from the database and populate any choice fields.
services.xml
<service id="app.form.modifier_supprimer_projet" class="AdminBundle\Form\ModifierSupprimerProjet">
<tag name="form.type" alias="app_modifier_supprimer_projet" />
<argument type="service" id="doctrine.orm.entity_manager"/>
</service>
ModifierSupprimerProjet.php
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityManager;
class ModifierSupprimerProjet extends AbstractType
{
/**
* @var EntityManager
*/
protected $em;
/**
* @param EntityManager $em
*/
public function __construct(EntityManager $em)
{
$this->em = $em;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('project', 'choice', [
'label' => 'form.project',
'choices' => $this->em->getRepository('AppBundle:Project')->getProjectsList(),
])
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults([
'data_class' => 'AppBundle\Entity\ModifierSupprimerProjet',
]);
}
/**
* @return string
*/
public function getName()
{
return 'app_modifier_supprimer_projet';
}
}
Controller
$modifierSupprimerProjet = new ModifierSupprimerProjet();
$form = $this->createForm('app_modifier_supprimer_projet', $modifierSupprimerProjet);
Second param of createForm method is Entity, if your form is mapped to any entity.
A while ago I had to do something similar, I had to get the list of icons and pass that to the form and this is how I achieved it.
inside my action
// my entity
$services = new Services();
$icons = the query for icons, in your case the projects
// creating form view, notice 'icons' being passed to the form
$form = $this->createForm(new AddServices(), $services, array('icons' => $icons));
Inside your form class
namespace Acme\YourBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class AddServices extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
//build your form here
$builder->add('....');
//form field linked with your dropdown options, in your case the projects
$builder->add('icon', 'choice', array(
'choices' => $options['icons'],
'required' => false,
));
}
public function getName()
{
return 'addServices';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
//notice icons being set here as default options
$resolver->setDefaults(array(
'icons' => null,
));
}
}
Then inside your twig {{ form_widget(form.icon, { 'attr': {'class': ''} }) }}
I am sure there might be other ways to achieve this but this is how I do it and seems to work perfectly fine
We assume that you've already read the documentation, but couldn't understand how to use it.. It's very simple, there are 3 important methods in form type, buildForm
which builds the fields of your form, setDefaultOptions
for versions <=2.6 or configureOptions
for versions >2.6 where you specify options which are consumed by buildForm
method (internally by Form Component), getName
- unique name of your form (here is a simple clarification to this method).
You can solve your task like this if we go back to your problem:
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver
->setDefaults([
'data_class' => 'YOUR_ENTITY' /* FormComponent autmatically maps the form fields with your entity if you set this option and pass an object as an 2-arguement to `createForm` method in your controller */
'my_custom_option' => null /* here we specify our custom option and set its initial value as null */
])
->setRequired([ /* this is optional, which tells the 'my_custom_option' option is required */
'my_custom_option'
])
->setAllowedTypes([ /* this one is also optional, which specify the type your 'my_custom_option' option */
'my_custom_option' => 'Symfony\Component\HttpFoundation\Request' // this is an example, which allows only `request` object
])
;
}
public function buildForm(...)
{
$myCustomOption = $options['my_custom_option']; // here we get our 'my_custom_option'
....
}
In your controller:
$formulaire=$this->createForm(new ModifierSupprimerProjet(), OBJECT_OR_NULL /* here we set our object which we've already assigned to `data_class` or we set to NULL */, [
'my_custom_option' => $request
]);