如何在cake php中添加验证只允许字符?

时间:2015-01-14 12:53:14

标签: validation cakephp

我是cakephp的新手。我创建了一个表单来添加用户。我想只允许用户名字符。我的验证代码如下:

public $validate = array(
       "username" => array(
           "required" => array(
                "rule" => array("notEmpty"),
                "message" => "Name is required"
            ) 
        ),
        "age" => array(
           "required" => array(
                "rule" => array("notEmpty"),
                "message" => "Age is required"
            )
        )
);

我应该添加什么才能只允许用户名字符。

1 个答案:

答案 0 :(得分:0)

使用the "custom" rule,它采用正则表达式:

"rule" => array('custom', '[a-zA-Z]'),

请参阅Regex to match only letters

CakePHP Validation manual page.