2014-10-10 111 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()窗体的方法