FOSUserBundle を使用しており、ユーザーがユーザー プロファイルを更新できるページを作成しようとしています。私が直面している問題は、ユーザーがパスワードを変更/更新したくない場合に、フォームでパスワードを再入力する必要がないことです。したがって、ユーザーが空のパスワードでフォームを送信すると、データベースは空の文字列で更新され、ユーザーはログインできなくなります。
パスワード フィールドが設定されていない場合、フォームにパスワード フィールドの更新を無視させるにはどうすればよいですか? 以下は私が使用しているコードです。
$user = $this->get('security.context')->getToken()->getUser();
//user form has email and repeating password fields
$userForm = $this->createForm(new UserFormType(), $user);
if ($request->getMethod() == 'POST') {
$userForm->bindRequest($request);
if($userForm->isValid()){
//this will be be empty string in the database if the user does not enter a password
$user->setPlainPassword($userForm->getData()->getPassword());
$em->flush();
}
}
次のようないくつかのことを試しましたが、bindRequest が空のパスワードをユーザーに設定するため、これはまだ空です。
if($userForm->getData()->getPassword())
$user->setPlainPassword($userForm->getData()->getPassword());
私も試しましたが、これは同様の状況になり、不要なクエリが発生します
if($userForm->getData()->getPassword())
$user->setPlainPassword($userForm->getData()->getPassword());
else
$user->setPlainPassword($user->getPlainPassword());
このユースケースを処理するエレガントな方法はありますか?