2014-10-10 110 views
0

我對這個Symfony框架不熟悉,並且在實施過程中遇到了死衚衕。僅當輸入用戶的current password時,我才需要驗證new passwordconfirm password字段。基於另一個字段的驗證

我盡力通過去,雖然這些鏈接理解的概念,

但事實證明無論是所使用的類已棄用或要求一個實體。

兩個領域的實施情況如下,

//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; 
     } 

    } 
+0

我可以看到你如何創建表單(所以澈'$這個 - >的CreateForm()')方法基本上 – DonCallisto 2014-10-10 07:04:46

+0

@DonCallisto的回調方法謝謝你的回覆,我創建瞭如下形式:$ form = $ this-> createForm(new MyAccountForm(),$ myAccountUser,array('action'=> $ this-> generateUrl('accounts_myaccount')));'' – 2014-10-10 07:11:23

+0

那麼,你可以在這個問題中包含'$ myAccountUser'類嗎?有點「運氣」,我想我已經得到了答案 – DonCallisto 2014-10-10 07:20:04

回答

0

修改類如下

use Symfony\Component\Validator\Constraints as Assert; 
use Symfony\Component\Validator\ExecutionContext; 

/** 
* 
* @Assert\Callback(methods={"passwordVerify"}) 
*/ 
class User implements UserInterface, \Serializable { 
    //all your old code here 
    public function passwordVerify(ExecutionContext $context) 
    { 
    //your controls about password fields here 
    //in case of failure you can add that snippet of code 
    $context->addViolationAtPath($propertyPath,'your message here', array(), null); 
    } 
} 

當然,你必須能夠訪問所有信息進入passwordVerify函數,最快的方法是創建字段verifyPassword到你的實體中,所以當你將form與實體綁定時,所有的數據都會在那裏。

這代碼片段會自動調用當您使用isValid()窗體的方法