为了账号安全,请及时绑定邮箱和手机立即绑定

仅当 symfony 表单类型上其他字段不为空时才验证

仅当 symfony 表单类型上其他字段不为空时才验证

PHP
PIPIONE 2023-08-06 10:49:44
我是 symfony 表单类型的新手。我有一种情况,在表单中我需要包含更改密码功能我的表单类型如下<?phpnamespace App\Form;use App\Entity\User;use Symfony\Component\Form\AbstractType;use Symfony\Component\Form\FormBuilderInterface;use Symfony\Component\OptionsResolver\OptionsResolver;use Symfony\Component\Validator\Constraints\Image;use Symfony\Component\Form\Extension\Core\Type\HiddenType;use Symfony\Component\Form\Extension\Core\Type\PasswordType;use Symfony\Component\Form\Extension\Core\Type\RepeatedType;use Symfony\Component\Form\Extension\Core\Type\FileType;use Symfony\Component\Validator\Constraints\NotBlank;use Symfony\Component\Validator\Constraints\Length;use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;class ProfileFormType extends AbstractType{    public function buildForm(FormBuilderInterface $builder, array $options)    {        $imageConstraints = [            new Image([                'maxSize' => '2M'            ])        ];        $builder            ->add('firstName')            ->add('lastName')            ->add('imageFile', FileType::class, [                'mapped' => false,                'label' => false,                'required' => false,                'error_bubbling' => true,                'constraints' => $imageConstraints            ])            ->add('imageFileName', HiddenType::class, [                'mapped' => false,            ])            ->add('oldPassword', PasswordType::class, array('label'=>'Current password', 'mapped' => false,            'required' => false,'error_bubbling' => true,'constraints' => new UserPassword([                'message' => "Please enter user's current password",                ])))我已经成功实现了该功能。但我的问题是我每次提交表单时都需要输入文件,oldPassword否则它会根据用户当前密码的需要给出验证错误。我想更改它,因为仅当输入新密码时,我才需要验证提交的旧密码。有什么可能的方法来实现这一点。希望有人可以帮助..
查看完整描述

1 回答

?
蝴蝶不菲

TA贡献1810条经验 获得超4个赞

如果要修改字段的验证方法,可以使用 Symfony FormEvents

有了这个,您可以将 EventListener 添加到 oldPassword 字段:

 $builder

            ->add(

                'oldPassword',

                PasswordType::class,

                array(

                    'label' => 'Current password',

                    'mapped' => false,

                    'required' => false,

                    'error_bubbling' => true,

                    // without constraint, so the form can be submitted without

                )

            )

            ->addEventListener(

                FormEvents::PRE_SUBMIT,

                function (FormEvent $event) use ($options) {

                    // if plainPassword is set, then overwrite the form field with check

                    if (isset(($event->getData())['plainPassword'])) {

                        $form = $event->getForm();

                        $form->add(

                            'oldPassword',

                            TextType::class,

                            [

                                'label' => 'Current password',

                                'required' => true,

                                'constraints' => new UserPassword(

                                    [

                                        'message' => "Please enter user's current password",

                                    ]

                                ),

                            ]

                        );

                    }

                }

            );


查看完整回答
反对 回复 2023-08-06
  • 1 回答
  • 0 关注
  • 50 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信