How to use select box related on another select box?的答案幫助我開始了,如果將第一個選擇框的ID硬編碼到第二個選擇框中,我的代碼就可以工作。但是,如果我嘗試使用實際的形式價值,我得到一個顯示基於Symfony 2.1中另一個選擇框的選擇框的內容
注意:未定義指數: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');
}
)
);
如果我改變$選項[「my_group」]到實際的數字是該組的ID,它會顯示正確。下面是HTML代碼輸出時,我居然把一個ID代替$選項[「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>
對我在做什麼錯誤或遺漏任何幫助嗎?
----道歉我非常遲來的迴應,因爲我沒有看到您的帖子(不過謝謝你。)這是我的的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存儲你的表單類型的選項。在控制器中創建它時,您可以將選定的組注入到表單中。你能發佈你的控制器代碼嗎? – tamir