密码有自定义验证器吗? zend框架

时间:2012-08-12 16:39:44

标签: php zend-framework passwords

我想询问是否有办法验证我的密码,以确定它是否包含至少1个使用zend表格验证器的字母,数字和符号。

据我所知,只有alpha,alphanum等:http://framework.zend.com/manual/1.7/en/zend.validate.set.html

2 个答案:

答案 0 :(得分:6)

这是我使用的自定义密码验证程序。您可以传递一系列选项以满足密码要求,并且可以根据您的选项返回一个解释密码要求的字符串。

用法:

$passwordOpts = array('requireAlpha' => true,
                      'requireNumeric' => true,
                      'minPasswordLength' => 8);

$pwValidator = new My_Validator_SecurePassword($passwordOpts);

$password = new Zend_Form_Element_Password('password', array(
    'validators' => array($pwValidator),
    'description' => $pwValidator->getRequirementString(),
    'label' => 'Password:',
    'required' => true,
));

验证器输出的示例要求字符串如下所示:

  
    

密码长度必须至少为8个字符,包含一个数字且包含一个字母字符。

  

验证器:

<?php

class My_Validator_SecurePassword extends Zend_Validate_Abstract
{
    const ALL_WHITESPACE = 'allWhitespace';
    const NOT_LONG       = 'notLong';
    const NO_NUMERIC     = 'noNumeric';
    const NO_ALPHA       = 'noAlpha';
    const NO_CAPITAL     = 'noCapital';

    protected $_minPasswordLength = 8;
    protected $_requireNumeric    = true;
    protected $_requireAlpha      = true;
    protected $_requireCapital    = false;

    protected $_messageTemplates = array(
        self::ALL_WHITESPACE => 'Password cannot consist of all whitespace',
        self::NOT_LONG       => 'Password must be at least %len% characters in length',
        self::NO_NUMERIC     => 'Password must contain at least 1 numeric character',
        self::NO_ALPHA       => 'Password must contain at least one alphabetic character',
        self::NO_CAPITAL     => 'Password must contain at least one capital letter',
    );

    public function __construct($options = array())
    {        
        $this->_messageTemplates[self::NOT_LONG] = str_replace('%len%', $this->_minPasswordLength, $this->_messageTemplates[self::NOT_LONG]);

        if (isset($options['minPasswordLength'])
            && Zend_Validate::is($options['minPasswordLength'], 'Digits')
            && (int)$options['minPasswordLength'] > 3)
            $this->_minPasswordLength = $options['minPasswordLength'];

        if (isset($options['requireNumeric'])) $this->_requireNumeric = (bool)$options['requireNumeric'];
        if (isset($options['requireAlpha']))   $this->_requireAlpha   = (bool)$options['requireAlpha'];
        if (isset($options['requireCapital'])) $this->_requireCapital = (bool)$options['requireCapital'];

    }

    /**
     * Validate a password with the set requirements
     * 
     * @see Zend_Validate_Interface::isValid()
     * @return bool true if valid, false if not
     */
    public function isValid($value, $context = null)
    {
        $value = (string)$value;
        $this->_setValue($value);

        if (trim($value) == '') {
            $this->_error(self::ALL_WHITESPACE);
        } else if (strlen($value) < $this->_minPasswordLength) {
            $this->_error(self::NOT_LONG, $this->_minPasswordLength);
        } else if ($this->_requireNumeric == true && preg_match('/\d/', $value) == false) {
            $this->_error(self::NO_NUMERIC);
        } else if ($this->_requireAlpha == true && preg_match('/[a-z]/i', $value) == false) {
            $this->_error(self::NO_ALPHA);
        } else if ($this->_requireCapital == true && preg_match('/[A-Z]/', $value) == false) {
            $this->_error(self::NO_CAPITAL);
        }

        if (sizeof($this->_errors) > 0) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * Return a string explaining the current password requirements such as length and character set
     * 
     * @return string The printable message explaining password requirements
     */
    public function getRequirementString()
    {
        $parts = array();

        $parts[] = 'Passwords must be at least ' . $this->_minPasswordLength . ' characters long';

        if ($this->_requireNumeric) $parts[] = 'contain one digit';
        if ($this->_requireAlpha)   $parts[] = 'contain one alpha character';
        if ($this->_requireCapital) $parts[] = 'have at least one uppercase letter';

        if (sizeof($parts) == 1) {
            return $parts[0] . '.';
        } else if (sizeof($parts) == 2) {
            return $parts[0] . ' and ' . $parts[1] . '.';
        } else {
            $str = $parts[0];
            for ($i = 1; $i < sizeof($parts) - 1; ++$i) {
                $str .= ', ' . $parts[$i];
            }

            $str .= ' and ' . $parts[$i];

            return $str . '.';
        }
    }
}

希望这有助于某人。

答案 1 :(得分:1)

Zend Framework中没有预制的Validator验证器。

但是,如果您在Writing validators查看 示例#3 ,您会找到一个密码验证器应该是什么的好例子。

效果很好。我自己使用它的一个版本。