使用$ form-> createView()渲染表单时,Symfony 4.x ROLE_USER错误

时间:2019-03-28 17:59:25

标签: symfony-4.2

我正在用Symfony 4创建一个博客,并使用:

  

php bin /控制台make:form

当我尝试像这样渲染它时:

    /**
    *   Require ROLE_USER for only this controller method.
    *   @Route("/create", name="post.create")
    *   @return Response
    */
    public function create(): Response
    {
        $post = new Post();
        $form = $this->createForm(CreatePostType::class, $post);

        return new Response($this->twig->render('pages/create.html.twig'), [
            'form' => $form->createView()
        ]);
    }

我收到此错误

  

无法将数据库值“'ROLE_USER'”转换为Doctrine类型json

此行

  

'form'=> $ form-> createView()

这是我的getRoles:

    /**
     * @see UserInterface
     */
    public function getRoles(): array
    {
        $roles = $this->roles;

        // guarantee every user at least has ROLE_USER
        $roles[] = 'ROLE_USER';

        return array_unique($roles);
    }

和我的security.yaml

security:
    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        in_memory: { memory: ~ }
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            anonymous: ~

我要搜索3天,然后在没有安全管理的情况下在第二个目录中重新启动我的项目,我不知道它来自何处以及如何解决

谢谢

1 个答案:

答案 0 :(得分:0)

我猜Roles属性没有以json-type存储在数据库中。您可以检查数据库中角色字段的类型吗?

作为旁注,您可以将响应重写为:

return $this->render('pages/create.html.twig', [
  'form' => $form->createView(),
]);