vendredi 3 avril 2015

FOSUserBundle permission form

I try add field: permission to edit group. I used: Add roles field to FOSUserBundle group form [Solved]


Now I have problem with save it.


First option:



public function editAction($groupname)
{
$group = $this->findGroupBy('name', $groupname);
$form= $this->container->get('fos_user.group.form'); // I can't use fos_user.group.form.factory, why?
$formHandler = $this->container->get('fos_user.group.form.handler');

$process = $formHandler->process($group);
if ($process) {
$this->setFlash('fos_user_success', 'group.flash.updated');
$groupUrl = $this->container->get('router')->generate('fos_user_group_show', array('groupname' => $group->getName()));

return new RedirectResponse($groupUrl);
}
$form->add('roles', 'choice', array(
'choices' => $this->getExistingRoles(),
'data' => $group->getRoles(),
'label' => 'Uprawnienia',
'expanded' => true,
'multiple' => true,
'mapped' => true,
));

return $this->container->get('templating')->renderResponse('FOSUserBundle:Group:edit.html.'.$this->getEngine(), array(
'form' => $form->createview(),
'groupname' => $group->getName(),
));
}


It nice show. See groups and checkbox's. But when I want sent this form (save date) I see:



You cannot add children to a submitted form



Second option:


I deleted $form->add('roles', 'choice', array... I changed the form: GroupFormType



public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', null, array('label' => 'form.group_name', 'translation_domain' => 'FOSUserBundle'));
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$form = $event->getForm();
$entity = $event->getData();
if ($entity instanceof Group) {
$form->add('roles', 'choice', array(
'choices' => $entity[]....,
'data' => $entity[]....
'label' => 'Permission',
'expanded' => true,
'multiple' => true,
'mapped' => true,
));
}

});
}


How I can sent $event with editAction() to addEventListener?? I tried: $this->createForm($group) but symfony2 won't let me


Aucun commentaire:

Enregistrer un commentaire