简体   繁体   中英

sfValidatorDoctrineUnique and sha1 in Symfony?

In User.class.php i have:

$this->setCode(sha1($this->getPassword()));

This is ok. In register form i would like use sfValidatorDoctrineUnique .

$this->validatorSchema->setPostValidator(new sfValidatorDoctrineUnique(array('model'=>'User', 'column'=>'code')));

But this doesnt work. This check password from form without sha1 and from database with sha1. I use sfValidatorDoctrineUnique also for email (without sha1) and this working ok. How can i fix it? I use Symfony 1.4.12

Why the hell would you check if the password is unique? this makes totally no sense

UPD: For creating an own validator create a /lib/valdator/sfValidatorDoctrineFoobar.class.php

<?php

class sfValidatorDoctrineFoobar extends sfValidatorBase
{
  protected function configure($options = array(), $messages = array())
  {
    $this->addMessage('invalid_record', 'Unable to find the related record');
  }

  protected function doClean($value){
    $status = $this->getCodeStatus($value);
    if ($status == 1){
     throw new sfValidatorError($this, 'Code is invalid', array('value' => 'invalid code'));
    }

    if ($status == 2){
     throw new sfValidatorError($this, 'Code allready used', array('value' => 'used'));
    }

    return $value;
  }

  protected function getCodeStatus($value){
    $q = Doctrine_Query::create()->from('Code c');
    $q->select('c.hash, c.used');
    $q->addWhere('c.hash = ?', $value);

    $result = $q->fetchOne(array(), Doctrine::HYDRATE_ARRAY);

    if (!$result) return 1;
    if ($result['used'] == 1) return 2;
    return false;
  }
}

just to show you an example.. you'll have to change the code to your needs ;)

Symfony has a built in option for hashing your passwords I think that you should use that. You can read about it here: sfGuardPlugin . If I were you I would be using the sfDoctrineGuardPlugin in tandem with the sfForkedDoctrineApplyPlugin . I was using that and it took care of all those problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM