set selected values for entitytype - symfony

I've have a formtype that has an entitytype field.
$builder->add('canRead', EntityType::class, [
'attr' => ['style' => 'height:150px;'],
'choice_value' => 'id',
'required' => true,
'multiple' => true,
'expanded' => false,
'class' => 'AppBundle\Entity\User',
'query_builder' => function (EntityRepository $er) {
$qb = $er->createQueryBuilder('e');
$qb->innerJoin('e.roles', 'r')->where('r.role LIKE :role')->setParameter('role', 'ROLE_ADMIN_%')
->orderBy('e.lastName', 'ASC')
->addOrderBy('e.firstGame', 'ASC');
return $qb;
},
]);
In the controller, I call this like that:
$form = $this->createForm(MedicalType::class, null, [ 'data'=>[] ]);
Where the 'data' is meant to be the selected values for the generated multiple select-field.
I've tried to pass a single User object, an array of User id's, arraycollection of Users, etc.. All of my tries before was the part from "query_builder"s results of course.
Did not work neither of them. Does someone know a solution to pass "selected" values for an entitytype field in formbuilder?
Thank you in advance.

The second parameter to createForm() being null seems to be the problem.
Lets say you have an array of user objects:
$data = [$user1, $user2];
Then try creating the form like that:
$form = $this->createForm(MedicalType::class, $data);
or this way:
$form = $this->createForm(MedicalType::class, array(), [ 'data'=> $data ]);

I tried the above solution but it didn't work for me so this might be helpful for someone else.
I had to pass an array of modules to the form so that it will be the default selected values, so in my controller i did this:
function testAction(){
$modules=array();
//here i'm just loading the modules that will be the default selected values
for ($i=7;$i<20;$i++){
$modules[]=$this->getDoctrine()->getRepository("AppBundle:Module")->find($i);
}
//passing the modules to the form
$form=$this->createForm(ModuleFieldType::class, array(), [ 'data'=> $modules ]);
return $this->render("/Security/test.html.twig",array("form"=>$form->createView()));
}
And in my form I did this :
$builder->add('module', EntityType::class, array(
'class' => 'AppBundle\Entity\Module',
'choice_label' => 'libelle',
'label' => 'Modules Enseignés',
'placeholder'=>"selectionez les modules enseigné",
'group_by' => function (Module $module) {
return $module->getDepartement()->getName();
},
"attr"=>array("id"=>"modulesSelect"),
'multiple' => true,
//you need add this to the form in order to read the data
'data'=>$options['data'],
));
The result was like that in the rendered form:
The rendered form

Related

Symfony 3.3 EntityType using distinct values in a drop down

I am working on an existing Symfony 3.3 project, and I have been asked to register a dropdown field in a results filter form that should contain all the values from a field in the database.
Until now, what I have do is the following that partially works:
$builder->add(
'newField',
EntityType::class,
[
'class' => Transaction::class,
'required' => false,
'multiple' => false,
'label' => 'New Field',
'choice_label' => 'newField',
'placeholder' => 'Choose a value'
]
)
The above code is able to display the values correctly, but unfortunately, because the table has repeatedly the same values, I get a long list of all the same values.
Instead, what I need to achieve is to display the DISTINCT values from the database.
I have tried the query_builder, but to be honest I am not sure I use it correctly as I am not an experienced Symfony developer.
Any idea on how to display the entries of the given column with DISTINCT results?
As you said yourself, you can use the query builder to achieve this. Something as simple as this should achieve what you're after:
->add('fooBar', EntityType::class, [
'query_builder' => function (EntityRepository $repository) {
return $repository
->createQueryBuilder('f')
->distinct();
},
]);
You can also specify a flag to be distinct and remember not to return the result but return the query. You can read more here
You are right,
Query_builder is the way to go.
So first create the method of what you want to do in TransactionRepository.
class TransactionRepository extends EntityRepository
{
//....
public function getDistinctValues()
{
//Return what you want to get, I cannot write this for you without your entity
}
}
Then use it in the FormType :
$builder->add(
'newField',
EntityType::class,
[
'class' => Transaction::class,
'required' => false,
'multiple' => false,
'label' => 'New Field',
'choice_label' => 'newField',
'placeholder' => 'Choose a value',
'query_builder' => function(TransactionRepository $repository) {
return $repository->getDistinctValues();
}
]
)
And you should be good to go.

Creating array from an object in symfony2

I'm trying to access a list of categories I have in a database and put them into a form in Symfony2.
public function productAddAction()
{
$product = new Product();
$categories = $this->getDoctrine()
->getRepository('AudsurShopBundle:Category')
->findAll();
$form = $this->createFormBuilder($product)
->add('category', 'choice', array(
'choices' => $categories, /* this is wrong */
'multiple' => false,
))
->add('name', 'text')
->add('save', 'submit', array('label' => 'Create Task'))
->getForm();
return $this->render('AudsurAdminBundle:Default:new.html.twig', array(
'form' => $form->createView(),
));
}
How do I go from $categories to an object that I can put into the following part, and it complying with what the function expects?
->add('category', 'choice', array(
'choices' => $categories, /* this is wrong */
'multiple' => false,
))
I know this is basic, but I can't seem to find the right keywords to find the answer (what should I have looked for?)
First off, "this is wrong" is not a specific error message for us to help you very much. It's like saying "my code doesn't work" and not telling us why. Moving on to your actual problem..
You're not using the right Form type to handle the entity type and display it properly. As #Talus mentioned, the Field Type you need is entity. There's a few things you're missing, such as the class parameter and the property parameter (assuming you haven't written a __toString() function in the Entity class.)
$categories = $this->getDoctrine()
->getRepository('AudsurShopBundle:Category')
->findAll();
$form = $this->createFormBuilder($product)
->add('category', 'entity', array(
'class' => 'AudsurShopBundle:Category',
'choices' => $categories,
'multiple' => false,
'property' => 'name', // This needs to be an actual property, I just assumed name
))
->add('name', 'text')
->add('save', 'submit', array('label' => 'Create Task'))
->getForm();
Since you're using all Category entities that exist, the findAll() query is actually not necessary. You can instead go for the basic usage:
$form = $this->createFormBuilder($product)
->add('category', 'entity', array(
'class' => 'AudsurShopBundle:Category',
'multiple' => false,
'property' => 'name', // This needs to be an actual property, I just assumed name
))
->add('name', 'text')
->add('save', 'submit', array('label' => 'Create Task'))
->getForm();
If you're looking for a specific subset of the Categories, you can use the choices property like before or pass a query_builder.
IRC, I think there is a type for that : http://symfony.com/fr/doc/current/reference/forms/types/entity.html
This should help to whatever you want to do, if I understood what you meant. :)
Well you can parse an Array from database:
Create a method in repository findAllArray():
public function findAllArray() {
return $this->getEntityManager()
->createQuery(
'SELECT Category '.
'FROM AudsurShopBundle:Category AS Category'
)
->getArrayResult();
}
Then call it in your controller and get all categories:
$categories = $this->getDoctrine()
->getRepository('AudsurShopBundle:Category')
->findAllArray();
When you have got array, make it suitable for choice (create new array $choices):
$choices = array();
foreach ($categories as $category) {
$choices[$value['id']] = $value['title'];
}
Then put this new array into form:
->add('category', 'entity', array(
'choices' => $choices,
Hope it helped. Have a nice day.

symfony form builder update option field

is it possible to update an option field after adding it ?
$builder
->add('examens', 'entity', array(
'class' => 'TelegrammeExamenBundle:ExamExamen',
'property' => 'libelle',
'required' => true,
'empty_value' => 'Sélectionnez un examen',
//'data' => $this->em->getReference("TelegrammeExamenBundle:ExamExamen", 510),
'data' => null,
'query_builder' => function(ExamenRepository $r) {
return $r->getSelectList();
},
'attr' => array('class' => 'bg_white_filet_gris')
))
;
how modify field option ??? (setOption don't exist)
if (...) $builder->get('examens')->setOption('property', 'test');
You can simply ->add() it again. As the API documentation suggests for the add method:
Adds or replaces a child to the form
http://api.symfony.com/2.8/Symfony/Component/Form/FormInterface.html#method_add
This can be used to modify form elements for example in a FormEvent.
Alternatively the FormBuilder provides a setAttribute() method which can be used as follows:
$builder->get('examens')->setAttribute('property', 'test');

Set default value for radio button

My buildForm class is like this below.
$builder->add('icon','entity',
array(
'class' => 'UserBundle:IconPics',
'property' => 'label', // .. or whatever property the image location is stored.
'expanded' => true,
'multiple' => false,
'label' => 'form.icon', 'translation_domain' => 'FOSUserBundle',
'query_builder' => function ($repository) {
return $repository->createQueryBuilder('i')
->add('where', 'i.enabled = true');
}
));
How can I set the default vaule for this radiobutton?
According to Peter Bailey's answer
use Acme\UserBundle\Entity\IconPics;
//
$IconPics = new IconPics();
// howw can I select the target Icon?????
You can set the initial data for a form in a couple ways
With the data option
'data' => $default
Where $default is an instance of UserBundle\Entity\IconPics
Providing an initial data source
// Assuming form is created in a controller
$data = new WhateverYourEntitiyIs();
$defaultIcon = new \UserBundle\Entity\IconPics();
// Set properties on $defaultIcon here
// or select $defaultIcon via the repository
$data->setIcon( $defaultIcon );
$form = $this->createForm( 'form_type', $data );

Overriding multiple select in symfony 2 formbuilder

So, I have a formbuilder action which has, in part, this:
->add('apples_group', 'entity',
array(
'class' => 'ThingMainBundle:ApplesGroup',
'multiple' => true,
'expanded' => true,
'property' => 'applesName'
)
)
This currently outputs as a multi-select box in this code:
{{ form_widget(form.apples_group, { "attr": {"class": "fields-list" } }) }}
Sometimes - in a different View - this needs not to be multiple, and should be a dropdown box instead. Is there a way to specify to the widget that the attribute multiple should be false when you render this widget?
(Validation of if this allowed to be multiple in this circumstance is already on the back end)
It is better to create
// YourFormType.php
public function __construct(Apple $apple)
{
$this->apple = $apple;
}
and
->add('apples_group', 'entity',
array(
'class' => 'ThingMainBundle:ApplesGroup',
'multiple' => $this->apple->isMultiple() , // you set it in admin, right?
'expanded' => true,
'property' => 'applesName'
)
)
controller:
$entity = new Apple() ;
$form = $this->createForm( new YourFormType($entity), $entity) ;
So it looks like the actual answer is to wait until https://github.com/symfony/symfony/issues/6602 lands.

Resources