2011-08-19 73 views
0

User.class.php我有:sfValidatorDoctrineUnique和sha1在Symfony中?

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

这是确定。以注册形式,我想使用sfValidatorDoctrineUnique

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

但是这不起作用。这个检查来自没有sha1的表单和来自sha1的数据库的密码。我使用sfValidatorDoctrineUnique也用于电子邮件(没有sha1),这工作正常。 我该如何解决它?我使用Symfony 1.4.12

回答

1

为什么你会检查密码是否是唯一的?这使得完全没有意义

UPD:用于创建自己的验证创建/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; 
    } 
} 

只是向你展示一个例子..你就必须改变代码根据您的需要;)

+0

我需要这个。我必须使这也为用户的唯一代码。 –

+2

然后再添加一些熵:-)像$ this-> setCode(sha1($ this-> getPassword()。$ this-> getId()。$ this-> getUsername()。time())); –

+0

我可以做到这一点。我将修改验证器唯一原则 –

0

Symfony有一个用于散列密码的内置选项我认为你应该使用它。你可以在这里阅读:sfGuardPlugin。如果我是你,我会使用sfDoctrineGuardPluginsfForkedDoctrineApplyPlugin一起使用。我正在使用它,它处理了所有这些问题。

+0

我想这也代码,不仅为密码 –

+0

然后,你应该写一个自定义验证。 –

+0

但是如何?任何例子? –