Answer to How to use a selection field associated with another selection field? helped me get started, and my code works if I hardcode the identifier of the first selection field in the second selection field. However, if I try to use the actual form value, I get
Note: Undefined index: my_group
Here is the code for the dropdown I want to display based on the group selection dropdown:
$builder->add('subscriptiontype', 'entity', array( 'label' => 'profile.edit.my_subscription_type', 'translation_domain' => 'FOSUserBundle', 'empty_value' => 'Select subscription type', 'property' => 'subscription_title', 'class' => 'SDMarketplaceBundle:SubscriptionType', 'multiple' => false, 'attr' => array('onchange' => '', 'class' => ''), 'query_builder' => function($repository) use ($options){ return $repository ->createQueryBuilder('j') ->where('j.isActive = :active AND j.valid_until > :timenow AND j.group = :group_id') ->setParameter('active', 1) ->setParameter('timenow', new \DateTime('now')) ->setParameter('group_id', $options['my_group']) ->orderBy('j.subscription_title', 'ASC'); } ) );
If I change $ options ['my_group'] to the actual number, which is the group identifier, it displays correctly. Here is the output of the html code when I actually put the id instead of $ options ['my_group'] :
<select id="fos_user_profile_form_my_group" name="fos_user_profile_form[my_group]" required="required"><option value="">Select group</option><option value="5">administrator</option><option value="2" selected="selected">instructor</option><option value="3">proofreader</option><option value="1">student</option><option value="4">translator</option></select>
Any help on what I'm doing wrong or missing?
---- I apologize for my extremely late reply, since I did not see your message (but thanks.) Here is my controller code for showAction:
public function showAction() { $user = $this->container->get('security.context')->getToken()->getUser(); if (!is_object($user) || !$user instanceof UserInterface) { throw new AccessDeniedException('This user does not have access to this section.'); } $myGroup = $this->getEntityManager()->getRepository('SDMarketplaceBundle:Groups')->findOneBy(array('id' => $user->getMyGroup())); //retrieve the group selected by this user $subscriptiontype = $this->getEntityManager()->getRepository('SDMarketplaceBundle:Subscriptiontype')->findOneBy(array('id' => $user->getSubscriptiontype())); //retrieve the subscription type title for this user $accounttype = $this->getEntityManager()->getRepository('SDMarketplaceBundle:AccountType')->findOneBy(array('id' => $user->getMyAccountType())); //retrieve the account type name for this user return $this->container->get('templating')->renderResponse('FOSUserBundle:Profile:show.html.'.$this->container->getParameter('fos_user.template.engine'), array( 'user' => $user, 'myGroup' => $myGroup, 'subscriptiontype' => $subscriptiontype->getSubscriptionTitle(), 'accounttype' => $accounttype->getAccountName(), 'statusCode' => $user->getStatusName($user->getStatusCode()) )); }