Cakephp validation and regular expressions

cakephp-1.3

I am working in cakephp1.3. i have to validate my form using cakephp validation in model.i added a regular expression to restrict special characters entry.

My code is written below:

var $validate = array(
        'name' => array(
            'unique'=>array(
                 'rule'      => 'isUnique',
                    'message'   => 'Already taken',
            ),
            'pattern'=>array(
                 'rule'      => '/^[a-z]$/i',
                    'message'   => 'Only letters allowed',
            ),
            'empty'=>array(
                'rule' => 'notEmpty',
                'message' => 'This field cannot be left blank',
            ))
    );

rule 'unique' is not working. it was working fine before i added the 'pattern' rule in script.what is the issue? i try to place the order of rules i mentioned above, i thought there is may be some positioning issue but nothing happened.

plz help me..

Best Answer

Replace $ with +:

var $validate = array(
    'name' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            'message' => 'This field cannot be left blank',
        ),
        'unique'=>array(
            'rule' => 'isUnique',
            'message' => 'Already taken'
        ),
        'pattern'=>array(
             'rule'      => '[a-zA-Z]+',
             'message'   => 'Only letters allowed',
        ),
    ),
);
Related Topic