How to use select box related on another select box?의 대답은 시작에 도움이되고 첫 번째 선택 상자의 ID를 두 번째 선택 상자에 하드 코드하면 작동합니다. 내가 실제 형태로 값을 사용하려고한다면, 나는Symfony 2.1의 다른 선택 상자를 기반으로 선택 상자의 내용을 표시합니다.
주의를 얻을 : 정의되지 않은 인덱스 : my_group 여기
입니다 내가 원하는 드롭 다운 상자에 대한 코드 그룹을 기준으로 표시하는 방법 선택 드롭 박스 : 나는이 그룹의 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');
}
)
);
가 올바르게 표시됩니다.
<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>
내가 잘못하고 또는 실종 무엇에 어떤 도움 : 여기에 내가 실제로 $ 옵션 [ 'my_group'] 대신에 ID를 넣어 HTML 코드 출력은?
---- 난 당신의 게시물을 참조 (하지만 감사합니다.)하지 않았다으로 여기에 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