【问题标题】:Display contents of Select Box based on another Select Box in Symfony 2.1基于 Symfony 2.1 中的另一个 Select Box 显示 Select Box 的内容
【发布时间】:2012-10-14 12:07:40
【问题描述】:

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())
        ));
    }

【问题讨论】:

  • 为什么my_group 应该存储在$options 中? buildForm() $options 存储表单类型的选项。当您在控制器中创建选定的组时,您可以将其注入到您的表单中。你能发布你的控制器代码吗?

标签: jquery symfony listbox symfony-2.1


【解决方案1】:
猜你喜欢
  • 2018-06-04
  • 1970-01-01
  • 2021-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-13
  • 1970-01-01
相关资源
最近更新 更多