我的数据库中有一个 description
字段,它接受字母数字字符和空格,以及换行符。我想在我的 Server
模型中设置该验证规则。
这是我现在拥有的:
<?php
class Server extends AppModel {
public $validate = array(
// ...
'description' => array(
'characters' => array(
'rule' => 'alphaNumeric',
'message' => 'Alphanumeric characters only'
)
),
// ...
);
}
如何修改它以允许空格和换行以及字母数字字符?我以为 Cake 会有一个内置的验证规则,但我看不到。我需要自己编写并使用一些正则表达式吗?
谢谢。
最佳答案
CakePHP 中的 alphaNumeric 表示没有空格的字母数字字符。因此,您必须使用自己的正则表达式模式。
例子:
'description' => array(
'characters' => array(
'rule' => array('custom', '/^[a-z0-9 ]*$/i'),
'message' => 'Alphanumeric characters with spaces only'
)
),
关于php - CakePHP 验证规则 : alphanumeric with spaces,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11320691/