symfony表单“此值不应为空”错误

pnwntuvh  于 2023-04-21  发布在  其他
关注(0)|答案(2)|浏览(155)

我有一个实体和一个表单,我想删除密码字段。

#[ORM\Column(
        type: 'string',
        name: 'password',
        length: 20,
        nullable: false
    )]
    #[Assert\NotBlank]
    #[Assert\Length(
        max: 20
        nullable: true
    )]
    private string $password

我将其更改为下面的一个,并生成和运行迁移。

#[Gedmo\Versioned]
    #[ORM\Column(
        type: 'string',
        name: 'password',
        length: 20,
        nullable: true
    )]
    private string $password;

删除它从窗体生成器以及小枝视图,但我得到**'这个值不应该是空白错误'**当提交表单。

mqkwyuun

mqkwyuun1#

如果它是可空的,那么你应该试试这个

private ?string $password;
wn9m85ua

wn9m85ua2#

您需要使用验证组,以便可以排除密码字段。
你甚至可以一直包含密码字段-但是不要在NotBlank验证它。在你的控制器中,你测试是否为空,只有当不散列新密码时。
例如:

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): FormBuilderInterface
    {
        $builder
            ->add('name', TextType::class, ['label' => 'Name', 'required' => false])
            ->add('email', EmailType::class, ['label' => 'Email'])
            ->add(
                'plainPassword',
                RepeatedType::class,
                [
                    'type' => PasswordType::class,
                    'label' => 'Password',
                    'invalid_message' => 'user.password.mustmatch',
                    'first_options' => ['label' => 'Password'],
                    'second_options' => ['label' => 'Password repeat'],
                ]
            )
            ->add('role', ChoiceType::class, [
                'label' => 'Role',
                'placeholder' => 'generic.select',
                'choices' => UserRoleInterface::FORM_USER_ROLES_ADMIN,
                // 'attr' => ['class' => 'select2'],
            ])

        ;

        return $builder;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults(
            [
                'data_class' => User::class,
                'validation_groups' => function (Form $userType) {
                    $user = $userType->getData();
                    if (null === $user->getId()) {
                        return ['Default', 'Create'];
                    }

                    return ['Default'];
                },
            ]
        );
    }
}

查看验证组-如果我们没有ID,则处于“创建”模式,密码字段是强制性的。如果有ID,则处于编辑模式-并且密码不必再次设置。
并且作为示例,用户实体:

#[UniqueEntity('email', groups: ['create'])]
class User implements UserInterface, UserEmailInterface, PasswordAuthenticatedUserInterface, UserRoleInterface, UserPasswordResetInterface
{
    protected ?string $id = null;

    #[Assert\NotBlank(groups: ['create', 'update-password'])]
    #[Assert\Length(min: 8, groups: ['create', 'update-password'])]
    protected ?string $plainPassword = null;

    #[Assert\NotBlank(groups: ['create', 'update-password', 'Default'])]
    #[Assert\Email(groups: ['create', 'update-password', 'Default'])]
    #[Assert\Length(max: 200, groups: ['create', 'update-password', 'Default'])]
    protected ?string $email = null;

    protected ?string $password = null;
    protected array $roles = [];
    protected ?string $role = null;
    [...] 
}

相关控制器部分:

if ($form->isSubmitted() && $form->isValid()) {
    if (null !== $user->getPlainPassword()) {
        $passwordHelper->updateUserPassword($user);
    }
    $manager->flush();
}

相关问题