0
我對這個Symfony
框架不熟悉,並且在實施過程中遇到了死衚衕。僅當輸入用戶的current password
時,我才需要驗證new password
和confirm password
字段。基於另一個字段的驗證
我盡力通過去,雖然這些鏈接理解的概念,
- http://shout.setfive.com/2013/06/27/symfony2-forms-without-an-entity-and-with-a-conditional-validator/
- Validate form fieldA based on either fieldA or fieldB in Symfony 2
- http://tomislavsantek.iz.hr/2011/03/using-symfony-postvalidator/
但事實證明無論是所使用的類已棄用或要求一個實體。
兩個領域的實施情況如下,
//if this field is filled
$builder->add('currentPassword', 'password', array('label'=>'Current Password',
'required'=>false,
'attr'=>array('class'=>'form-control'),
'error_bubbling' => true,
'trim' => true,
'mapped' => false,
'label_attr'=>array('class'=>'col-sm-4 control-label')));
//These repeated fields must be filled or must be set as required
$builder->add('password', 'repeated', array('type' => 'password',
'required' => false,
'invalid_message' => ErrorMessages::PASSWORDS_DO_NOT_MATCH,
'options' => array('attr' => array('class' => 'password-field form-control')),
'first_options' => array('label' => false,
'error_bubbling' => true,
'label_attr'=>array('class'=>'col-sm-4 control-label')),
'second_options' => array('label' => false,
'label_attr'=>array('class'=>'col-sm-4 control-label'))));
我使用控制器內的一堆if
條件實施了驗證,但它會是巨大的,學習的情況進行驗證的正確方法如此。 :)
謝謝
編輯
用戶entity
<?php
namespace Proj\Bundle\AccountsBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\User\UserInterface;
use Proj\Bundle\AccountsBundle\Custom\ErrorMessages;
class User implements UserInterface, \Serializable {
/**
* @Assert\Email(message=ErrorMessages::EMAIL_ADDRESS_INVALID)
* @Assert\NotBlank(message=ErrorMessages::EMAIL_ADDRESS_EMPTY)
*/
private $email;
/**
* @Assert\NotBlank(message=ErrorMessages::PASSWORD_EMPTY, groups={"full"})
*/
private $password;
private $oldPassword;
private $id;
private $userId;
private $name;
private $username;
public function __construct() {
}
function setEmail ($email) {
$this->email = $email;
$this->username = $email;
}
function getEmail() {
return $this->email;
}
function setPassword ($password) {
$this->password = $password;
}
function getPassword() {
return $this->password;
}
function setOldPassword ($oldPassword) {
$this->oldPassword = $oldPassword;
}
function getOldPassword() {
return $this->oldPassword;
}
function setId ($id) {
$this->id = $id;
}
function getId() {
return $this->id;
}
function setUserId ($userId) {
$this->userId = $userId;
}
function getUserId() {
return $this->userId;
}
function setName (PersonName $name) {
$this->name = $name;
}
function getName() {
return $this->name;
}
public function eraseCredentials() {
}
public function getRoles() {
return array('ROLE_USER');
}
public function getSalt() {
}
public function getUsername() {
return $this->username;
}
}
我可以看到你如何創建表單(所以澈'$這個 - >的CreateForm()')方法基本上 – DonCallisto 2014-10-10 07:04:46
@DonCallisto的回調方法謝謝你的回覆,我創建瞭如下形式:$ form = $ this-> createForm(new MyAccountForm(),$ myAccountUser,array('action'=> $ this-> generateUrl('accounts_myaccount')));'' – 2014-10-10 07:11:23
那麼,你可以在這個問題中包含'$ myAccountUser'類嗎?有點「運氣」,我想我已經得到了答案 – DonCallisto 2014-10-10 07:20:04