我正在努力学习symfony表格。我想为用户构建一个表单。此用户作为收集产品(nom,description)的arraycollection字段。
我想创建一个表单,为每个传递给表单构建器的产品创建一个复选框。实际上它只创建了没有标签的输入字段......这是我的UserType类的代码:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('nom')->add('prenom')->add('mail')->add('tel1')->add('tel2', TextType::class, array('required' => false))
->add('username')->add('password', PasswordType::class)
->add('groupe', ChoiceType::class, array(
'choices' => array('Administrateur' => 'ROLE_SUPER_ADMIN', 'Gérant' => 'ROLE_ADMIN', 'Opérateur' => 'ROLE_USER'),
'expanded' => true,
))
->add('produits', CollectionType::class, array(
'entry_type' => ProduitType::class
))
;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => User::class
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'gestcoupons_userbundle_user';
}
这是我的ProduuctType代码:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('nom')->add('prenom')->add('mail')->add('tel1')->add('tel2', TextType::class, array('required' => false))
->add('username')->add('password', PasswordType::class)
->add('groupe', ChoiceType::class, array(
'choices' => array('Administrateur' => 'ROLE_SUPER_ADMIN', 'Gérant' => 'ROLE_ADMIN', 'Opérateur' => 'ROLE_USER'),
'expanded' => true,
))
->add('produits', CollectionType::class, array(
'entry_type' => ProduitType::class
))
;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => User::class
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'gestcoupons_userbundle_user';
}
这是我的UserController代码:
public function ajouterAction(Request $request){
$this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!');
$em = $this->getDoctrine()->getManager();
$produits = $em->getRepository('ProduitBundle:Produit')->findAll();
$societes = $em->getRepository('SocieteBundle:Societe')->findAll();
$user = new User();
foreach ($produits as $produit) {
$user->getProduits()->add($produit);
}
$form = $this->createForm('GestCoupons\UserBundle\Form\UserType', $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush($user);
return $this->redirect('dashboard_admin');
}
return $this->render('user/new.html.twig', array(
'user' => $user,
'form' => $form->createView(),
));
}
先谢谢你的帮助。
答案 0 :(得分:1)
您应该使用 Symfony \ Bridge \ Doctrine \ Form \ Type \ EntityType 而不是CollectionType。
在您的UserType
中use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use AppBundle\Entity\Produit;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nom')
->add('prenom')
->add('mail')
->add('tel1')
->add('tel2', TextType::class, array(
'required' => false
))
->add('username')
->add('password', PasswordType::class)
->add('groupe', ChoiceType::class, array(
'choices' => array(
'Administrateur' => 'ROLE_SUPER_ADMIN',
'Gérant' => 'ROLE_ADMIN',
'Opérateur' => 'ROLE_USER'
),
'expanded' => true,
))
->add('produits', EntityType::class, array(
'class' => Produit::class,
'multiple' => true,
'expanded' => true,
'label' => 'nom' //If you don't have a __toString method in your Produit Entity
))
;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => User::class
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'gestcoupons_userbundle_user';
}
通过这种方式,您的数据库中的每个产品实体都会有一个复选框,只有您检查的实体才会与您的用户相关联