根据Symfony 2.1中的另一个选择框显示选择框的内容

时间:2012-10-24 12:40:12

标签: jquery symfony listbox symfony-2.1

How to use select box related on another select box?的答案帮助我开始了,如果我将第一个选择框的id硬编码到第二个选择框中,我的代码就可以运行了。但是,如果我尝试使用实际的表单值,我会得到一个

  

注意:未定义的索引:my_group

以下是我想根据组选择下拉框显示的下拉框的代码:

  $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');
                    }
        )
    );

如果我将 $ options ['my_group'] 更改为实际的数字,即该组的ID,则会正确显示。这是我实际用id取代 $ options ['my_group'] 时的html代码输出:

<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>

我正在做错误或遗失的任何帮助?

----因为我没有看到你的帖子而感到非常迟来的回应道歉(但是谢谢你。)这是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())
        ));
    }

0 个答案:

没有答案