我是 symfony 表单类型的新手。我有一种情况,在表单中我需要包含更改密码功能我的表单类型如下
<?php
namespace 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否则它会根据用户当前密码的需要给出验证错误。我想更改它,因为仅当输入新密码时,我才需要验证提交的旧密码。
有什么可能的方法来实现这一点。希望有人可以帮助..
蝴蝶不菲