Zend Framework Source Code Snippets

Password Validator

Bookmark and Share
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
5556
57
58
59
6061
62
63
64
6566
67
68
69
7071
72
73
74
7576
77
78
79
8081
82
83
84
8586
87
88
89
9091
92
93
94
9596
97
98
99
100101
102
103
104
105106
107
108
109
110111
112
113
114
115116
117
118
119
120121
122
123
124
125126
127
128
129
130131
132
133
134
135136
137
138
<?php
 
class My_Validate_Password
        extends Zend_Validate_Abstract
{ 
    /**
     * constantes
     *
     */    const NOUPPERCASE = 'noUppercase' ;
    const NOLOWERCASE = 'noLowercase' ;
    const NODIGIT = 'noDigit' ;
    const TOOSHORT = 'tooShort' ;
     /**
     * options
     *
     * @var array|Zend_Config|null
     */    public $options ;
    /**
     * @var array
     */
    protected $_messageVariables = array (        'min' => '_min' ,
            ) ;
    /**
     *
     * @var integer     */
    protected $_min ;
    /**
     * variable des messages
     *     * @var array
     */
    protected $_messageTemplates = array (
        self :: NOUPPERCASE => 'Votre mot de passe doit contenir au moins une majuscule' ,
        self :: NOLOWERCASE => 'Votre mot de passe doit contenir au moins une minuscule' ,        self :: NODIGIT => 'Votre mot de passe doit contenir au moins un chiffre' ,
        self :: TOOSHORT => 'Votre mot de passe doit faire plus de %min% caractères'
            ) ;
 
    /**     * constructor
     *
     * clés possibles pour les options :
     *
     * 'uppercase' => at least one uppercase letter required     * 'lowercase' => at least one lowercase letter required
     * 'digit' => at least one digit required
     * 'min' => minimum string length
     *
     * @param array|Zend_Config|null $options     */
    public function __construct ( $options=NULL )
    {
        if ( $options instanceof Zend_Config ) {
             $options = $options -> toArray () ;
        }
 
        if ( is_array ( $options ) ) {
             $this -> setOptions ( $options ) ;
        }
 
    }
     /**
     * validation
     *
     * @param string $string
     * @return bool     */
    public function isValid ( $string )
    {
        $return = array ( ) ;
         if ( isset ( $this -> options[ 'lowercase' ] ) && $this -> options[ 'lowercase' ] == 1 ) {
 
            if ( ! preg_match ( '`^.*[a-z]+.*$`' , $string ) ) {
                $this -> _error ( self :: NOLOWERCASE ) ;
                $return[ ] = FALSE ;            }
        }
 
        if ( isset ( $this -> options[ 'uppercase' ] ) && $this -> options[ 'uppercase' ] == 1 ) {
             if ( ! preg_match ( '`^.*[A-Z]+.*$`' , $string ) ) {
                $this -> _error ( self :: NOUPPERCASE ) ;
                $return[ ] = FALSE ;
            }
        } 
        if ( isset ( $this -> options[ 'digit' ] ) && $this -> options[ 'digit' ] == 1 ) {
 
            if ( ! preg_match ( '`^.*[0-9]+.*$`' , $string ) ) {
                $this -> _error ( self :: NODIGIT ) ;                $return[ ] = FALSE ;
            }
        }
 
        if ( isset ( $this -> options[ 'min' ] ) && is_numeric ( $this -> options[ 'min' ] ) ) { 
            $this -> _min = $this -> options[ 'min' ] ;
 
            $strlenValidator = new Zend_Validate_StringLength ( array ( 'min' => $this -> options[ 'min' ] ) ) ;
             if ( ! $strlenValidator -> isValid ( $string ) ) {
                $this -> _error ( self :: TOOSHORT , $this -> options[ 'min' ] ) ;
                $return[ ] = FALSE ;
            }
        } 
        if ( in_array ( FALSE , $return ) ) {
 
            return FALSE ;
        } 
        return TRUE ;
 
    }
     /**
     *
     * @param array $options
     */
    public function setOptions ( $options )    {
        $this -> options = $options ;
 
    }
 }
 
?>

Comments

You must login before commenting on a snippet. If you do not have an account, please register.

Snippet description

Password validator with configurable options, located in library/My/Validate.

Messages templates are in french, because I am... You\'ve got to adapt them.

Example in a .ini file form a form :

...
elements.password.options.validators.password.validator = "Password"
elements.password.options.validators.password.options.lowercase = 1
elements.password.options.validators.password.options.uppercase = 1
elements.password.options.validators.password.options.digit = 1
elements.password.options.validators.password.options.min = 8
...

Snippet details

Created:
Web82 Web82
4 months ago
Edited:
Web82 Web82
4 months ago
Revision Id:
212
Edit Message:
Initial Release
ZF Version
1.10.7
Tags:
zend_validate zend_form
Comments:
0
Views:
1112
Points:
1 (1 votes)

History

r212

Initial Release

Web82 Web82
4 months ago
diff
r211

Initial Release

Web82 Web82
4 months ago
diff
r210

Initial Release

Web82 Web82
4 months ago