4
我想在兩個條件來更新數據:如何從表單中刪除了symfony表單處理空值字段
- 當用戶在表單中的所有字段(姓名,電子郵件,密碼)
- 當用戶沒有輸入密碼時(我只需更新名稱&電子郵件)。
我有以下formHandler
方法。
public function process(UserInterface $user)
{
$this->form->setData($user);
if ('POST' === $this->request->getMethod()) {
$password = trim($this->request->get('fos_user_profile_form')['password']) ;
// Checked where password is empty
// But when I remove the password field, it doesn't update anything.
if(empty($password))
{
$this->form->remove('password');
}
$this->form->bind($this->request);
if ($this->form->isValid()) {
$this->onSuccess($user);
return true;
}
// Reloads the user to reset its username. This is needed when the
// username or password have been changed to avoid issues with the
// security layer.
$this->userManager->reloadUser($user);
}
我認爲你可以使用Form事件來解決這個問題 –
@Wo子宮如果他想改變形式(如標題所示),但如果是數據(正如身體所說)? – cheesemacfly
我想改變窗體。如果密碼字段爲空,則要刪除密碼字段。然後提交剩餘的數據。 – user2506165