dpzlz08480 2014-10-10 06:04
浏览 118
已采纳

验证基于另一个字段

I am new to this Symfony framework and ran into a dead end during implementation. I am required to validate new password and confirm password fields only if current password of the user is entered.

I tried my best to understand the concept by going though these links,

But turns out either the classes used are deprecated or required an entity.

The implementation of the two fields are as follows,

//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')))); 

I implemented a validation using a bunch of if conditions within the controller but it would be great to learn the proper way of performing validations for a scenario such as this. :)

Thank you

EDIT

the user 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;
      }

    }
  • 写回答

1条回答 默认 最新

  • dpp78272 2014-10-10 08:29
    关注

    Modify your class as follows

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

    Of course you have to be able to access all informations into passwordVerify function and the quickest method to do this is to create the field verifyPassword into your entity so when you bind form with entity all data will be there.

    That snippet of code will call automatically a callback method when you use isValid() form's method

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助