使用者密碼
此驗證輸入值是否等於目前已驗證使用者的密碼。這在使用者可以更改密碼但需要輸入舊密碼以確保安全的表單中非常有用。
注意
不應使用此功能來驗證登入表單,因為這會由安全性系統自動完成。
注意
為了使用此約束條件,您應該已透過 Composer 安裝 symfony/security-core 元件。
適用於 | 屬性或方法 |
類別 | 使用者密碼 |
驗證器 | UserPasswordValidator |
基本用法
假設您有一個 ChangePassword
類別,用於使用者可以透過輸入舊密碼和新密碼來變更密碼的表單中。此約束條件將驗證舊密碼是否與使用者目前的密碼相符
1 2 3 4 5 6 7 8 9 10 11 12
// src/Form/Model/ChangePassword.php
namespace App\Form\Model;
use Symfony\Component\Security\Core\Validator\Constraints as SecurityAssert;
class ChangePassword
{
#[SecurityAssert\UserPassword(
message: 'Wrong value for your current password',
)]
protected string $oldPassword;
}
本作品,包括程式碼範例,皆以 Creative Commons BY-SA 3.0 授權條款授權。