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",
]
),
]
);
}
}
);
- 1 回答
- 0 关注
- 78 浏览
添加回答
举报