cakephp验证和正则表达式 [英] cakephp validation and regular expressions

查看:96
本文介绍了cakephp验证和正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Cakephp1.3中工作。我必须使用model中的cakephp验证来验证我的表单。我添加了一个正则表达式来限制特殊字符的输入。

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.

我的代码如下:

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帮助我。.

推荐答案

用$替换$:

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',
        ),
    ),
);

这篇关于cakephp验证和正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆