CakePHP验证规则:带空格的字母数字 [英] CakePHP validation rule: alphanumeric with spaces
问题描述
我的数据库中有一个描述
字段,该字段接受字母数字字符和空格以及换行符。我想在我的 Server
模型中设置该验证规则。
这是我现在拥有的:
<?php
类服务器扩展AppModel {
public $ validate = array (
// ...
'描述'=>数组(
'字符'=>数组(
'规则'=>'alphaNumeric',
'message'=>'仅字母数字字符'
)
),
// ...
);
}
如何修改此值以允许空格和换行以及字母数字字符?我以为Cake会为此建立验证规则,但我看不到。我需要自己编写并使用一些正则表达式吗?
谢谢。
alphaNumeric表示不带空格的字母数字字符。因此,您必须使用自己的正则表达式模式。
示例:
说明 => array(
'characters'=> array(
'rule'=> array('custom','/ ^ [a-z0-9] * $ / i'),
'message'=>'仅带空格的字母数字字符'
)
),
I have a description
field in my database which accepts alphanumeric characters and spaces, as well as new line characters. I want to set that validation rule in my Server
model.
Here's what I have now:
<?php
class Server extends AppModel {
public $validate = array(
// ...
'description' => array(
'characters' => array(
'rule' => 'alphaNumeric',
'message' => 'Alphanumeric characters only'
)
),
// ...
);
}
How can I modify this to allow spaces and new lines as well as alphanumeric characters? I thought Cake would have a build in validation rule for this but I can't see one. Do I need to write my own and use some regex?
Thanks.
alphaNumeric in CakePHP means alphanumeric characters without spaces. Therefore you have to use your own regex pattern.
Example:
'description' => array(
'characters' => array(
'rule' => array('custom', '/^[a-z0-9 ]*$/i'),
'message' => 'Alphanumeric characters with spaces only'
)
),
这篇关于CakePHP验证规则:带空格的字母数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!