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