Symfony dependent dropdown with 3 entities - symfony

So I'm about to create a form with three dropdowns which are interdependent.
When one or several channel1s are selected, the choices for channel 3 should change, according to what is selected for channel1. And dependent on that, the last dropdown "agencies" should change its choices.
I've already tried different solutions but none of them has worked so far. Right now I'm stuck on the solution provided by Symfony's documentation, which provides code for two entities but even with that one, my second dropdown doesn't have any values, so it's not working.
Here is my Form Type:
class SelectionType extends AbstractType {
protected $tokenStorage;
// private $manager;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
//solution Symfony Documentation
$channel1s = new Channel1();
$currentuser = $this->tokenStorage->getToken()->getUser();
$builder
->add('channel1s', EntityType::class, array(
'class' => 'AppBundle:Channel1',
'property' => 'name',
'label' => 'label.channel1s',
'empty_value' => 'label.select_channel1s',
'mapped' => false,
'expanded' => false,
'translation_domain' => 'UploadProfile',
'multiple' => true,
'required' => false,
));
$formModifier = function (FormInterface $form, Channel1 $channel1s = null) {
$channel3s = null === $channel1s ? array() : $channel1s->getChannel3();
$form->add('channel3s', EntityType::class, array(
'class' => 'AppBundle:Channel3',
'property' => 'name',
'label' => 'label.channel3s',
'empty_value' => 'label.select_channel3s',
'mapped' => false,
'expanded' => false,
'translation_domain' => 'UploadProfile',
'choices' => $channel3s,
'multiple' => true,
'choices_as_values' => true,
));
};
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($formModifier) {
$data = $event->getData();
$formModifier($event->getForm(), $data->getChannel1s());
}
);
$builder->get('channel1s')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($formModifier) {
$channel1s = $event->getForm()->getData();
$formModifier($event->getForm()->getparent(), $channel1s);
}
);
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'DocumentBundle\Entity\UploadProfile'
));
}
public function getName()
{
return 'uploadprofile';
}
}
I've also tried a solution with Subscribers from that page: http://showmethecode.es/php/symfony/symfony2-4-dependent-forms/ but it didn't work out either..
I think my problem is somewhere around that line:
$channel3s = null === $channel1s ? array() : $channel1s->getChannel3();
but that's just a guess..
I also added that ajax function:
var $channel1s = $('#uploadprofile_channel1s');
$channel1s.change(function() {
var $form = $(this).closest('form');
var data = {};
data[$channel1s.attr('name')] = $channel1s.val();
// data[channel3s.attr('name')] = channel3s.val();
$.ajax({
url : $form.attr('action'),
type: $form.attr('method'),
data : data,
success: function(html) {
$('#uploadprofile_channel3s').replaceWith(
$(html).find('#uploadprofile_channel3s')
);
}
});
});
My 3 entities have ManytoMany or OneToMany relationships and I should have all the right getters and setters, but if anyone needs them to varify, let me know and I will upload them!
I've been stuck on this for quite a while now, so I would be happy about any kind of help or advise!
NOTE: there's still a third entity (agency) to be added but since not even the first one's are working, I decided to upload only the first two..
ADDED:
or maybe somebody can explain to my that line:
$channel3s = null === $channel1s ? array() : $channel1s->getChannel3s();
might be, that this is my problem?

Related

Symfony getData event subscriber is null

I know this question has been asked already a couple of times, but there hasn't been an answer that actually helped me solving my problem.
I've got three EventSubscribers for three Dropdowns who are dependent on each other.
So in my FormType I say:
public function buildForm(FormBuilderInterface $builder, array $options)
{
// solution showmethecode
$pathToAgencies = 'agencies';
//
$builder
->addEventSubscriber(new AddChannel1Subscriber($pathToAgencies))
->addEventSubscriber(new AddChannel3Subscriber($pathToAgencies))
->addEventSubscriber(new AddAgencySubscriber($pathToAgencies));
}
and one of my EventSubscribers looks like that:
...
...
public static function getSubscribedEvents() {
return array(
FormEvents::PRE_SET_DATA => 'preSetData',
FormEvents::PRE_SUBMIT => 'preSubmit'
);
}
private function addChannel1Form($form, $channel1s = null) {
$formOptions = array(
'class' => 'AppBundle:Channel1',
'property' => 'name',
'label' => 'label.channel1s',
'empty_value' => 'label.select_channel1s',
'mapped' => false,
'expanded' => false,
'translation_domain' => 'UploadProfile',
'multiple' => true,
'required' => false,
'attr' => array(
'class' => 'channel1s'
),
);
if ($channel1s){
$formOptions['data'] = $channel1s;
}
$form->add('channel1s', 'entity', $formOptions);
}
public function preSetData(FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();
if (null === $data) {
return;
}
$accessor = PropertyAccess::createPropertyAccessor();
$agency = $accessor->getValue($data, $this->pathToAgency);
$channel1s = ($agency) ? $agency->getChannel3s()->getChannel1s() : null;
$this->addChannel1Form($form, $channel1s);
}
public function preSubmit(FormEvent $event) {
$form = $event->getForm();
$this->addChannel1Form($form);
}
...
Now I'm getting the error "Attempted to call an undefined method named "getChannel3s" of class "Doctrine\Common\Collections\ArrayCollection"." and (I think) this is because my $data in my preSetData is NULL but I don't know why it's null. Am I looking at the wrong spot or where is my mistake here?
preSetData is executed before the original data (which shall be modified if given) is bound to the form ( which is then stored in $options['data']).
The "data" in preSetData is the one you provide to createForm($type, $data = null, array $options = array()).
So before this is set -> the form obviously doesn't have any data and the event-data isn't set either. That's why $data is null inside your listener's onPreSetData method.
You're using the wrong event. Use preSubmit and build your logic around the data submitted by the user ($event->getData()). This will solve your issue.
Quick overview:
onPreSubmit:
$form->get('someButton')->isClicked() returns false
$event->getForm()->getData() returns $options['data'] if any or $options['empty_data']
$event->getData returns the submitted data (array)
you can use setData()
you can add/remove fields
onSubmit:
You can't use setData() here as data was already bound to the form
$form->isSubmitted() still returns false
$form->get('someButton')->isClicked() returns true
You can still add/remove fields
onPostSubmit:
$form->isSubmitted() returns true
"You cannot remove children from a submitted form"
"You cannot add children to a submitted form"
$form->get('someButton')->isClicked() returns true
In the preSetData declaration you get the bad class. Try this :
public function preSetData(GenericEvent $event)
Add the next use :
use Symfony\Component\EventDispatcher\GenericEvent;

Setting a default choice on Symfony's EntityType

How can I set a default choice on Symfony's EntityType, to use when the bound form object does not have a value?
I've tried the following (suggested in this answer), but the data option overwrites even a bound value.
class FooEntityType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
...
$resolver->setDefaults([
'choices' => $fooEntities,
'class' => 'FooBundle:FooEntity',
'choice_label' => 'name',
'expanded' => true,
'multiple' => false,
'data' => $fooEntities[0],
]);
}
public function getParent()
{
return EntityType::class;
}
}
you should move this code to buildForm(FormBuilderInterface $builder, array $options) method in your Type
try this
$entity = $options['data']; // this will be your entity
// form builder
$builder->add('entityProperty', EntityType::class, [
'choices' => $fooEntities,
'class' => 'FooBundle:FooEntity',
'choice_label' => 'name',
'expanded' => true,
'multiple' => false,
'data' => $entity->getEntityProperty() ? $entity->getEntityProperty() : $fooEntities[0]
]);
Also you can solve this workaround by form events, but it does not worth it
Once you create the form, you should add the entity to that method (assuming you are in a controller):
# src/Controller/MainController.php
$entity = new Entity();
$entity->setCertainValue('choice-value');
$form = $this->createForm(EntityType::class, $entity);
$form->handleRequest($request);
if ($form->isValid()) {
// Do something nice with the entity, data is filled here
}

Symfony2: formbuilder : dynamically modify querybuilder

I am using the formbuilder to create a form as followed:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('content', 'textarea')
->add('rosters', 'entity', array(
'class' => 'PlatformBundle:team',
'property' => 'display',
'multiple' => true,
'expanded' => true,
'required' => true
))
->add('send', 'submit')
;
}
At the moment I get all "teams". I need to adapt the form to display certain teams depending of the request.
I can use the query-builder inside the form builder
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('content', 'textarea')
->add('rosters', 'entity', array(
'class' => 'PlatformBundle:team',
'property' => 'display',
'query_builder' => function(TeamRepository $t) use ($userId) {
return $r->createQueryBuilder('t')
->where('(t.user = :user')
},
'multiple' => true,
'expanded' => true,
'required' => true
))
->add('send', 'submit')
;
}
But the query changes for different questionnaire. In brief: always the same questionnaire but different teams to be listed (Am I making sense?).
Does someone has an idea how dynamically modify the querybuilder inside a formbuilder?
I suggest two possible alternatives.
If the request comes from the form itself (i.e. you already submitted the form with some data and want to refine the fields) you can access the submitted data like this:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$data = $builder->getData();
// now you can access form data
If the request comes from another source, you should use the "options" parameter. First, build a new $option for the requested user:
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'user' => null,
));
}
Note: I set the default to null but you can set it to whatever you want.
After that you can pass the $option where you build the form, i.e.
// some controller
$option = array('user' => $request->get('user');
$teamForm = $this->createForm(new TeamType(), null, $options);
// ...
For those looking for an answer...
The best solution I found is create a variable in the formtype and to import it form the controller. My formType will look like that:
class formType extends AbstractType
{
// declare and construct the query in the class to use it in the function
private $qb;
public function __construct ($qb)
{
$this->qb = $qb;
}
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
// declare the variable within the function
$qb = $this->qb;
$builder
->add('content', 'textarea', array('required' => false))
->add('rosters', 'entity', array(
'class' => 'PlatformBundle:Team',
// use ($qb) -> $qb is query built in the controller (or repository)
'query_builder' => function(TeamRepository $r) use ($qb) {
return $qb;
},
'property' => 'display',
'multiple' => true,
'expanded' => true,
'required' => true
))
->add('send', 'submit');
}
In my controller I just pass $qb as an argument of the formtype
$qb = $this->getDoctrine()->getManager()->getRepository('PlatformBundle:Team')->qbteam($Id);
$form = $this->createForm(new formType($qb), $form);
with qbteam a function in the team repository which return the query (not a result).
public function qbteam($Id){
$qb = $this->createQueryBuilder('r')
->leftJoin('r.team', 'm')
->addSelect('m')
->where('m.user = :user')
->setParameter('user', $Id);
return $qb;
}
I hope it will help others.
cheers

Unit testing of forms with collections

I have almost no experience in terms of unit tests. I read this symfony cookbook chapter to test a form type.
http://symfony.com/doc/current/cookbook/form/unit_testing.html
My form look like this:
public function __construct(SecurityContext $securityContext, \Doctrine\ORM\EntityManager $em)
{
$this->securityContext = $securityContext;
$this->entityManager = $em;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('title', 'text', array('label' => 'title', 'translation_domain' => 'messages', 'attr' => array('maxlength' => 255)))
->add('comments', 'collection', array(
'type' => new CommentType() ,
'allow_add' => false,
'allow_delete' => false,
'label' => false,
'options' => array(
'label' => false,
)
)
)
->add('translations', 'a2lix_translations', array(
'fields' => array(
'coverLetter' => array(
'label' => 'msg.coverLetter',
'field_type' => 'textarea',
'attr' => array('class' => 'rte')
)
)
));
}
Now i write a class to test my form.
class QuestionnaireControllerTest extends TypeTestCase
{
public function testAddQuestionnaire()
{
$kernel = new \AppKernel('dev', true);
$kernel->boot();
$container = $kernel->getContainer();
$securityContext = $container->get('security.context');
$entityManager = $container->get('doctrine.orm.entity_manager');
$formData = array('title' => 'Exp. title');
$type = new QuestionnaireType($securityContext, $entityManager);
$form = $this->factory->create($type);
$form->submit($formData);
$this->assertTrue($form->isSynchronized());
$view = $form->createView();
$children = $view->children;
foreach (array_keys($formData) as $key) {
$this->assertArrayHasKey($key, $children);
}
}
}
But I have some questions for my test class.
Is this the correct way to get the kernel?
How can i test the form item "comments (collection)", "translations (a2lix_translations)"?
Unfortunately i do'nt find useful tutorials for these issues.
When it comes to TypeTest's it can become a little weird actually as it seems out of the loop. I faced some similar problems and actually did the following:
1) Regarding the Kernel: Extend the KernelTestCase and include TypeTestCase (and super) logic in your test (or create yourself an abstract). That way you will have a more consistent Kernel init way and TypeTestCase is not that big besides the form initialization. To have that customizable actually might be helpful later on.
2) To get especially the A2lix stuff loaded as extensions to your test forms you have to override
protected function getExtensions()
and return an array
return array(
new PreloadedExtension(..., array())
);
For A2lix ... might look like
$gedmoTranslationsType = new GedmoTranslationsType($this->container->get('a2lix_translation_form.gedmo.listener.translations'), $this->container->get('a2lix_translation_form.gedmo.service.translation'), $this->getLocales(), false);
$gedmoTranslationsLocalesType = new GedmoTranslationsLocalesType();
$translationsFields = new TranslationsFieldsType();
return array(
$gedmoTranslationsType->getName() => $gedmoTranslationsType,
$gedmoTranslationsLocalesType->getName() => $gedmoTranslationsLocalesType,
$translationsFields->getName() => $translationsFields,
);
I already got myself the container and locales in convenient members and functions. You should do so to.
In general the extensions will also become important with e.g. testing Entity types. Therefore, generalizing these things might be helpful.
There is a lot more stuff to Type testing actually that you will figure out over time. I hope this helps anyway.
Best

How to disable specific item in form choice type?

I have a form with choice field of entities from database:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('categories', 'document', array(
'class' => 'Acme\DemoBundle\Document\Category',
'property' => 'name',
'multiple' => true,
'expanded' => true,
'empty_value' => false
));
}
This form will produce the list of checkboxes and will be rendered as:
[ ] Category 1
[ ] Category 2
[ ] Category 3
I want to disable some of the items by value in this list but I don't know where I should intercept choice field items to do that.
Does anybody know an solution?
you can use the 'choice_attr' in $form->add() and pass a function that will decide wether to add a disabled attribute or not depending on the value, key or index of the choice.
...
'choice_attr' => function($key, $val, $index) {
$disabled = false;
// set disabled to true based on the value, key or index of the choice...
return $disabled ? ['disabled' => 'disabled'] : [];
},
...
Just handled it with finishView and PRE_BIND event listener.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('categories', 'document', array(
'class' => 'Acme\DemoBundle\Document\Category',
'property' => 'name',
'multiple' => true,
'expanded' => true,
'empty_value' => false
));
$builder->addEventListener(FormEvents::PRE_BIND, function (FormEvent $event) {
if (!$ids = $this->getNonEmptyCategoryIds()) {
return;
}
$data = $event->getData();
if (!isset($data['categories'])) {
$data['categories'] = $ids;
} else {
$data['categories'] = array_unique(array_merge($data['categories'], $ids));
}
$event->setData($data);
});
}
...
public function finishView(FormView $view, FormInterface $form, array $options)
{
if (!$ids = $this->getNonEmptyCategoryIds()) {
return;
}
foreach ($view->children['categories']->children as $category) {
if (in_array($category->vars['value'], $ids, true)) {
$category->vars['attr']['disabled'] = 'disabled';
$category->vars['checked'] = true;
}
}
}

Resources