我正在Symfony 2中建立一個Web應用程序,其中我有一個用戶實體類來保存註冊用戶。Symfony2 - 使用實體值作爲默認當表單提交空字段
每個用戶至少應該有一個用戶名和一個密碼,因此,我使用@Assert\NotBlank
驗證規則爲實體中的兩個字段。用戶名是靜態的,但是,能夠更改密碼是可取的。
因此,我正在構建一個表單,用戶可以在其中登錄更改其密碼(等等)。此表單利用Symfony中的重複字段類型進行密碼確認。
現在,在大多數情況下,我猜用戶不想更改密碼,而是更新其個人資料中的其他字段。因此,我希望它可以讓他們將密碼字段留空。但是,這會干擾NotBlank
驗證規則。
刪除NotBlank
驗證規則當然是一個選項。雖然,但我很難找到解決方案,因爲這會導致用戶以空白密碼結束的問題。
最後,據我所知,symfony Request對象在表單提交時自動將表單數據填充回User實體。
現在,我的問題是:有沒有什麼辦法,在空的密碼字段,使Symfony用數據庫中的現有密碼填充用戶實體而不是空白值?這將允許驗證通過,並且在保存時不會導致密碼的任何不希望的改變。
這是在該實體的密碼字段中的代碼
/**
* @var string
*
* @ORM\Column(name="password",type="string",length=255)
* @Assert\NotBlank()
* @Assert\Type(type="string")
* @Assert\Length(max="255")
* @Assert\NotEqualTo(value="password")
*/
private $password;
這是用於產生形式
private function initForm($user) {
return $this->createFormBuilder($user)
->add('name', 'text')
->add('mail', 'email')
->add('password', 'repeated', [
'type' => 'password',
'invalid_message' => 'The password fields must match.',
'first_options' => ['label' => false],
'second_options' => ['label' => false] //set to false to hide label in view
])
->add('save', 'submit')->getForm();
}
這是/profile/edit
頁面操作碼的代碼:
/**
* @ParamConverter("user",class="MyBundle:User")
*/
public function editAction(User $user, Request $request) {
$form = $this->initForm($user);
$form->handleRequest($request);
if ($form->isValid()) {
//update to database and redirect user back to profile_view
$this->getDoctrine()->getManager()->flush();
return $this->redirect($this->generateUrl('profile_view'));
}
return $this->render('MyBundle:User:edit.html.twig',
array('form' => $form->createView()));
}
在此先感謝您的幫助!
編輯
我也嘗試了「明顯」的做法,即temporarilly存儲在一個變量的原始密碼和「放」回來,如果提交表單後,這是空白:
/**
* @ParamConverter("user",
class="MyBundle:User")
*/
public function editAction(User $user, Request $request) {
$password = $user->getPassword();
$form = $this->initForm($user)->getForm();
$form->handleRequest($request);
if ($user->getPassword() == NULL) {
$user->setPassword($password);
$form = $this->initForm($user)->getForm(); //this is the problem
}
if ($form->isValid()) {
//update to database and redirect user back to profile view
$this->getDoctrine()->getManager()->flush();
return $this->redirect($this->generateUrl('profile_view'));
}
return $this->render('MyBundle:User:edit.html.twig',
array('form' => $form->createView()));
}
然而,這並不是因爲明顯的原因。當表單在if
聲明中重新初始化時,Symfony發現表單從未首先提交,即不執行驗證。並且添加$form->handleRequest($request)
讓我們回到密碼字段可能爲空的第一個問題。
雖然我發現有特殊之處。 Symfony驗證實體而不是表單。但刪除問題行$form->initForm($user)->getForm()
仍然無效,但實體應該有效。
經過一番持續的研究,我相信你是對的!我會接受它作爲一個答案..但如果有人應該找到一種方式,我都是耳朵! – walander
我完全忘了;非常感謝您的幫助! =) – walander