0

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

1 Answer 1

4

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'
        )
    )
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.