Use function to add choices to select form Symfony 2 - php

I'm currently developping a Symfony application (v2.8) with two main bundles : EasyAdmin and FOSUserBundle.
I have users with a particular role and I'm using a 'findBy' with an array to retrieve those users.
I would like to override the form of my admin controller to fill a select form choices with those users. I know it's possible to do it with an array but I want something dynamic if I add or delete some users.
I don't know how can I call a function from one of my controllers (or do I have to declare it as a service ?) to add choices to select form from a query.
Here is an example of what I would like :
$formBuilder->add('field_to_override', 'choice', array(
'choices' => **my_function**,
'multiple' => true,
'expanded' => true,
));
I hope I'm clear in my explanation.
Thank you in advance !

Consider using an EntityType field, which is a specialized form of Choice field which uses Doctrine entities for the choices.
It is possible to specify a custom query for the choices too, e.g.
$builder->add('users', EntityType::class, array(
'class' => 'AppBundle:User',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.username', 'ASC');
},
));

Related

In Symfony EntityType fields, how do you change the rendered OPTGROUP text?

Symfony has an EntityType field that allows a <select> field to be automatically populated by rows in a table. Going further, these rows can be categorized into <optgroup> clusters if the group_by option is given a property path.
Example:
$builder->add('categories', EntityType::class, [
'class' => PostCategory::class,
'group_by' => 'column_name',
'mapped' => FALSE,
]);
Given this code snippet, how would I change the rendered <optgroup> text? For example, how would I humanize (e.g. capitalize) it?

How to specify a target entity type with sonata_type_collection

I am using Sonata Page Bundle to build a set of page with information about items -- one item per page. On each page, I want to include multiple synonyms for the item. I have created a Synonym entity with two fields.
I now want to use an add() method within my configureFormFields definition to add a reference to my new Synonym entity. The following code yields an error saying that The current field 'Synonym' is not linked to an admin. Please create one for the target entity : '':
->add('Synonym', 'sonata_type_collection', array(
'label' => "Synonyms",
'cascade_validation' => true,
'required' => false
), array(
'edit' => 'inline',
'inline' => 'table'
))
... and I have tried modifying the code so it looks like:
'cascade_validation' => true,
'required' => false,
'target_entity' => 'AppBundle\Entity\Synonym'
), array(
... which results in the same error.
How do I tell my admin class that this is a reference to a set of Synonym objects that I want to edit inline?
Edit:
I have also tried
'targetEntity' => 'AppBundle\Entity\Synonym'
... with no luck. The application is still seemingly having trouble figuring out what the entity I'm targeting is.
An at least partial answer was found in adding an "admin_code" definition to my array, as described at
https://symfony.com/doc/master/bundles/SonataAdminBundle/reference/form_types.html
I still get an error, but it is now at least a different error.

Sonata Admin Bundle: Paginate one-to-many field using sonata_type_collection

I am using SonataAdminBundle to display one-to-many fields of an entity on the admin panel.
Since I have a lot of related entities I am getting a 503 error when displaying the admin form.
I can override the template to limit the number of children but I would like to load the rest in some sort of pagination.
Is there any way that this can be achieved without breaking the structure of SonataBundle ?
Below is my code:
public function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('communityHasMedia', 'sonata_type_collection',[],[
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
'required' => false
])
}

Validation using entity for a FormType inside a form

I have two entities: Company and Location. One company has one location (while one location may "have" multiple companies). Now when a user creates a company, I want him/her to able able, to create the location in the same form. So I use the following
$builder
->add('name', TextType::class, ['label' => 'company.name'])
->add('size', IntegerType::class, ['label' => 'company.size'])
->add( $builder->create('location', FormType::class, [
'label' => 'company.location',
'by_reference' => true,
'data_class' => 'AppBundle\Entity\Location',
])
->add('street', TextType::class, [
'label' => 'location.street',
])
->add('number', TextType::class, [
'label' => 'location.number',
])
This works fine in creating the form. Now it comes to validation. I added #Assert annotations for both entities in their respective files. While company validation works, location does not get automatically validated.
I managed to get validation by adding constraint properties to the new $builder->create('location') elements, but this means duplicated code (once in Entity and at least once in every form that needs location).
How can I solve it so the form gets validated by using the entity's annotation?
By default validation won't traverse to properties that are objects or collections. Use the valid constraint:
http://symfony.com/doc/current/reference/constraints/Valid.html
You cans set the traverse option for collections as well.

Is there any edit option in sonata_type_model?

I have a sonata_type_model field which you can see in the screenshot and in the code below. The form allows me to add or remove elements to the multiple model field. Is there any way where I can get a link to edit those models in the dropdown/autocomplete field?
->add('items', 'sonata_type_model', ['multiple' => true, 'by_reference' => false, 'required' => false])
If you want to use only the possibility of a sonata, this is impossible. It is necessary to customize the type. But if you a lucky man, try to add 4th argument ->add('items', 'sonata_type_model', ['multiple' => true, 'by_reference' => false, 'required' => false], array('edit' => 'inline')) This is not work for me, but I don't had any errors. You can try to use sonata_type_model_list if relation is toOne between entities.
To add editing capabilities you should use sonata_type_collection.
The Collection Type is meant to handle creation and editing of model collections. Rows can be added and deleted, and your model abstraction layer may allow you to edit fields inline.
See: Form Types

Categories