如何在用户/编辑上为管理员提供 SF/Sonata RepeatedType (pw)

Posted

技术标签:

【中文标题】如何在用户/编辑上为管理员提供 SF/Sonata RepeatedType (pw)【英文标题】:How to provide SF/Sonata RepeatedType (pw) on User/edit for Admins 【发布时间】:2020-08-13 15:01:31 【问题描述】:

我们正在使用 symfony 4.4 并使用奏鸣曲进行实体管理。我们实现了一个非常轻量级的用户管理,并且还可以在用户编辑页面的第二个选项卡中向用户添加产品。所以现在管理员想要将产品分配给用户,但问题是密码字段不允许这种行为。首先密码字段是必需的,但将其设置为 false 根本没有帮助,因为该字段只是被提交为空。

所以我尝试的一个解决方案是在我的管理类中使用 preUpdate 函数来防止该错误。

    public function preUpdate($object)

    /** @var EntityManagerInterface $em */
    $em = $this->getConfigurationPool()->getContainer()->get('Doctrine')->getManager();
    /** @var UserRepository $repository */
    $repository = $em->getRepository(User::class)->find($object->getId());

    $password = $object->getPassword();
    if (!empty($password)) 
        $salt = md5(time());

        $encoderservice = $this->getConfigurationPool()->getContainer()->get('security.encoder_factory');
        $encoder = $encoderservice->getEncoder($object);
        $encoded_pass = $encoder->encodePassword($object->getPassword(), $salt);
        $object->setSalt($salt);
        $object->setPassword($encoded_pass);
     else 
        $object->setPassword($repository->getPassword());
    

    return $object;


所以如果密码不为空 - 我们将获取给定的值并更新密码。如果该字段为空,我们使用现有密码,因此只需添加产品并更新用户实体即可。

这很好,但触发错误出现在 preUpdate 函数可以提供帮助之前。

所以给定的错误是:

Expected argument of type "string", "null" given at property path "password".

preUpdate 不生效。

这里的解决方案是什么?

完整的堆栈跟踪:

Symfony\Component\PropertyAccess\Exception\InvalidArgumentException:
Expected argument of type "string", "null" given at property path "password".

  at vendor/symfony/property-access/PropertyAccessor.php:198
  at Symfony\Component\PropertyAccess\PropertyAccessor::throwInvalidArgumentException('string', array(array('file' => '/var/www/vendor/symfony/property-access/PropertyAccessor.php', 'line' => 548, 'function' => 'setPassword', 'class' => 'App\\Entity\\User', 'type' => '->', 'args' => array(null)), array('file' => '/var/www/vendor/symfony/property-access/PropertyAccessor.php', 'line' => 114, 'function' => 'writeProperty', 'class' => 'Symfony\\Component\\PropertyAccess\\PropertyAccessor', 'type' => '->', 'args' => array(array(object(User)), 'password', null)), array('file' => '/var/www/vendor/symfony/form/Extension/Core/DataMapper/PropertyPathMapper.php', 'line' => 86, 'function' => 'setValue', 'class' => 'Symfony\\Component\\PropertyAccess\\PropertyAccessor', 'type' => '->', 'args' => array(object(User), object(PropertyPath), null)), array('file' => '/var/www/vendor/symfony/form/Form.php', 'line' => 632, 'function' => 'mapFormsToData', 'class' => 'Symfony\\Component\\Form\\Extension\\Core\\DataMapper\\PropertyPathMapper', 'type' => '->', 'args' => array(object(RecursiveIteratorIterator), object(User))), array('file' => '/var/www/vendor/symfony/form/Extension/HttpFoundation/HttpFoundationRequestHandler.php', 'line' => 109, 'function' => 'submit', 'class' => 'Symfony\\Component\\Form\\Form', 'type' => '->', 'args' => array(array(), true)), array('file' => '/var/www/vendor/symfony/form/Form.php', 'line' => 493, 'function' => 'handleRequest', 'class' => 'Symfony\\Component\\Form\\Extension\\HttpFoundation\\HttpFoundationRequestHandler', 'type' => '->', 'args' => array(object(Form), object(Request))), array('file' => '/var/www/vendor/sonata-project/admin-bundle/src/Controller/CRUDController.php', 'line' => 331, 'function' => 'handleRequest', 'class' => 'Symfony\\Component\\Form\\Form', 'type' => '->', 'args' => array(object(Request))), array('file' => '/var/www/vendor/symfony/http-kernel/HttpKernel.php', 'line' => 158, 'function' => 'editAction', 'class' => 'Sonata\\AdminBundle\\Controller\\CRUDController', 'type' => '->', 'args' => array(null)), array('file' => '/var/www/vendor/symfony/http-kernel/HttpKernel.php', 'line' => 80, 'function' => 'handleRaw', 'class' => 'Symfony\\Component\\HttpKernel\\HttpKernel', 'type' => '->', 'args' => array(object(Request), 1)), array('file' => '/var/www/vendor/symfony/http-kernel/Kernel.php', 'line' => 201, 'function' => 'handle', 'class' => 'Symfony\\Component\\HttpKernel\\HttpKernel', 'type' => '->', 'args' => array(object(Request), 1, true)), array('file' => '/var/www/public/index.php', 'line' => 25, 'function' => 'handle', 'class' => 'Symfony\\Component\\HttpKernel\\Kernel', 'type' => '->', 'args' => array(object(Request)))), 0, 'password')
     (vendor/symfony/property-access/PropertyAccessor.php:118)
  at Symfony\Component\PropertyAccess\PropertyAccessor->setValue(object(User), object(PropertyPath), null)
     (vendor/symfony/form/Extension/Core/DataMapper/PropertyPathMapper.php:86)
  at Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper->mapFormsToData(object(RecursiveIteratorIterator), object(User))
     (vendor/symfony/form/Form.php:632)
  at Symfony\Component\Form\Form->submit(array(), true)
     (vendor/symfony/form/Extension/HttpFoundation/HttpFoundationRequestHandler.php:109)
  at Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler->handleRequest(object(Form), object(Request))
     (vendor/symfony/form/Form.php:493)
  at Symfony\Component\Form\Form->handleRequest(object(Request))
     (vendor/sonata-project/admin-bundle/src/Controller/CRUDController.php:331)
  at Sonata\AdminBundle\Controller\CRUDController->editAction(null)
     (vendor/symfony/http-kernel/HttpKernel.php:158)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor/symfony/http-kernel/HttpKernel.php:80)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor/symfony/http-kernel/Kernel.php:201)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (public/index.php:25) 

我能以某种方式将 preUpdate 的行为应用到其他地方吗?

这是formMapper:

        $formMapper
        ->tab("Information")
            ->add('email', TextType::class, [
                'attr' => [
                    'readonly' => $emailDisabledState,
                ],
            ])
            ->add('password', RepeatedType::class, [
                'type' => PasswordType::class,
                'invalid_message' => 'The password fields must match.',
                'options' => ['attr' => ['class' => 'password-field']],
                'required' => false,
                'first_options'  => ['label' => 'Password'],
                'second_options' => ['label' => 'Repeat Password'],
            ])
            ->add('isActive', BooleanType::class)
            ->add('roles', ChoiceType::class, [
                'choices' => array_flip($flattendRoles),
                'multiple' => true,
                'expanded' => false,
            ])
            ->end()
        ->end()
        ->tab("Products")
            ->add("products", EntityType::class, [
                'class' => Product::class,
                'multiple' => true
            ])
            ->end()
        ->end();

非常感谢您的帮助

【问题讨论】:

【参考方案1】:

重复类型的声明似乎不错。

您不必在 preUpdate 中防止错误。

您的实体中的参数是可选的吗?

    public function setPassword(?string $password): self
    
        $this->password = $password;

        return $this;
    

【讨论】:

我无法真正回应,您在回答中问了我一个问题。该问题由不同的客户要求解决 - 我仍然认为 preUpdate 将是该用例的解决方案,但我目前无法真正使用该代码库进行测试......我希望稍后再回来。仍然 ty 输入

以上是关于如何在用户/编辑上为管理员提供 SF/Sonata RepeatedType (pw)的主要内容,如果未能解决你的问题,请参考以下文章

编辑宏以在桌面上为任何用户创建文件夹

在多租户环境中,如何在运行时在不同的 url(子域)上为不同的服务提供者提供不同的元数据?

如何在 Ubuntu 18 上为 python 3.7 安装 pip?

看AppCan移动管理平台如何助力企业移动化

数据库编辑后无法在我的 Heroku 应用程序上查看用户配置文件

在CentOS上为PHP安装Imagick和ImageMagick