我在symfony 2 + FOSUserBundle中创建新/编辑用户表单时遇到问题。我想让管理员从下拉列表或单选按钮列表中选择用户角色,但我似乎无法让它工作。我在这里找到了这个答案How can I pass a full security roles list/hierarchy to a FormType class in Symfony2?这是我能找到的最相关的东西,但它不起作用。
这是目前的UserType表单。我想从容器中获取角色但是我似乎无法在没有它抛出错误的情况下工作。这些角色将正确填充下拉列表,但它不会显示当前分配的角色,也不会允许更新信息,因为它希望它是一个数组$entity->addRoles(array('ROLE_SUPER_ADMIN'));
,但它只是作为一个字符串提交
namespace Wes\AdminBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class UserType extends AbstractType
{
private $roles;
public function __construct($roles) {
$this->roles = $roles;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username')
->add('email')
->add('enabled')
->add('roles', 'choice', array(
'choices' => $this->flattenArray($this->roles),
))
->add('firstName')
->add('lastName')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Wes\AdminBundle\Entity\User',
'roles' => null,
'userRole' => null,
));
}
public function getName()
{
return 'wes_adminbundle_usertype';
}
private function flattenArray(array $data)
{
$returnData = array();
foreach($data as $key => $value)
{
$tempValue = str_replace("ROLE_", '', $key);
$tempValue = ucwords(strtolower(str_replace("_", ' ', $tempValue)));
$returnData[$key] = $tempValue;
}
return $returnData;
}
}
这是控制器。
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('WesAdminBundle:User')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find User entity.');
}
$editForm = $this->createForm(new UserType($this->container->getParameter('security.role_hierarchy.roles')), $entity);
$deleteForm = $this->createDeleteForm($id);
return $this->render('WesAdminBundle:User:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Edits an existing User entity.
*
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('WesAdminBundle:User')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find User entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createForm(new UserType($this->container->getParameter('security.role_hierarchy.roles')), $entity);
$editForm->bind($request);
if ($editForm->isValid()) {
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('wes_admin_user_edit', array('id' => $id)));
}
return $this->render('WesAdminBundle:User:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
我已经打了好几天了,我似乎无法让它正常工作。有什么想法吗?
答案 0 :(得分:3)
使用FOSUserBundle提供的userManager而不是自定义持久性方法,因为在将数组存储到数据库之前需要对其进行序列化。
$userManager = $this->container->get('fos_user.user_manager');
$user = $userManager->findUserBy(array('id' => $id));
$editForm = $this->createForm(new UserType($this->container->getParameter('security.role_hierarchy.roles')), $user);
if ($editForm->isValid()) {
$userManager->updateUser($user);
return $this->redirect($this->generateUrl('wes_admin_user_edit',
array('id' => $id)));
}
有关详细信息,请参阅https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/user_manager.rst(已修改)。