Cakephp特殊字符验证

时间:2014-04-16 06:09:16

标签: php cakephp cakephp-2.0

我想在模型中设置验证规则,用户只在文本字段中输入特殊字符。请建议我正确解决。

演示代码

public function addValidations()
    {
        parent::addValidations();
        $this->validate['Field name'] = array
        (
            'notempty' => array
            (
                'rule'    => array('special char', 'msg',),
                'allowEmpty' => false,
                'message' => 'Enter Special character only.',
            ),
        );
    }

1 个答案:

答案 0 :(得分:1)

您可以定义Custom Regular Expression Validation

public $validate = array(
    'login' => array(
        'rule'    => '/^[\W]+$/',
        'allowEmpty' => false,
        'message' => 'Enter Special character only.'
    )
)
// '/^[\w.-]+$/' this will allow only special character.