如何创建一个字段(下拉列表),其中Pracownik列表由idPlachta(会话变量)显示?
public function buildForm(FormBuilderInterface $builder, array $options){
$session = $options['session'];
$builder
->setMethod('GET')
->add('idPracownik'); //this place
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Przydzial',
'session' => null,
));
}
public function getBlockPrefix()
{
return 'appbundle_przydzial';
}
答案 0 :(得分:0)
您可以创建自己的pracownik选择类型并在任何地方使用
class PracownikChoiceType extends AbstractType
{
/**
* @var SessionInterface
*/
private $session;
public function __construct(SessionInterface $session)
{
$this->session = $session;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setDefault('class', 'Your\Entity\ClassName')
->setDefault('query_builder', function (EntityRepository $repository) {
$queryBuilder = $repository->createQueryBuilder('o');
$queryBuilder
->select('o')
->where($queryBuilder->expr()->eq('o.plachtaId', ':id'))
->setParameter('id', $this->session->get('idPlachta'))
;
return $queryBuilder;
})
;
}
/**
* {@inheritdoc}
*/
public function getParent()
{
return EntityType::class;
}
}
要将session
服务注入此表单类型,您应将其创建为服务:
<service id="your.service.id" class="Your\Namespace\Form\Type\PracownikChoiceType">
<argument type="service" id="session" />
<tag name="form.type" />
</service>
现在,您可以在任何地方使用此表单类型,而不需要构建$choices
列表。
// Usage with `FormInterface` instance
$form
->add('plachta', PracownikChoiceType::class, array(
'label' => 'Plachta',
'attr' => array(
'class' => 'form-control',
'style' => 'margin-bottom: 15px'
)
))
;
// Usage with `FormBuilderInterface` instance
$formBuilder
->add('plachta', PracownikChoiceType::class, array(
'label' => 'Plachta',
'attr' => array(
'class' => 'form-control',
'style' => 'margin-bottom: 15px'
)
))
;