dipanshu
dipanshu

Reputation: 197

cakephp code is not working for alphanumeric validation

we have tried several solution for validation of password,but none is working but user get login, all validations get working except alphanumeric validation in password.

Here is code:

'password' => array ('required' => array (

    'rule' => array ('notEmpty'),
    'rule' => array ('between',1,15 ),

    //'rule'    => array('custom', '[a-zA-Z0-9, ]+'),
    'message' => 'A password is required,must be between 8 to 15 characters' )
), 

using custom function it doesn't work so we tried

'alphaNumeric' => array(
    'rule'     => array('alphaNumericDashUnderscore'),
    'rule'     => 'alphaNumeric',
    'required' => true,
    'message'  => 'password must contain Alphabets and numbers only'
)),

custom function in model

public function alphaNumericDashUnderscore($check) {
    $value = array_values($check);
    $value = $value[0];

    return preg_match('|^[0-9a-zA-Z_-]*$|', $value);
}

we are working on cakephp version 2.4.3

Upvotes: 0

Views: 831

Answers (1)

noslone
noslone

Reputation: 1289

That is because you are defining two times the same key rule in an array. The second one will always overwrite the first one.

As per documentation, you should do it as follow:

public $validate = array(
    'password' => array(
        'password-1' => array(
            'rule'    => 'alphaNumeric',
            'message' => 'password must contain Alphabets and numbers only',
         ),
        'password-2' => array(
            'rule'    => 'alphaNumericDashUnderscore',
            'message' => 'password must contain Alphabets and numbers only'
        )
    )
);

Upvotes: 4

Related Questions