CakePHP - 我可以 $validate 一个字段以要求它没有空格吗? (它可以有下划线和连字符......等)
在 CakePHP 的模型中,我可以 $validate 一个字段来要求它没有空格或其他特殊字符吗?
具体来说,我希望他们为其在线文件夹输入一个名称 - 因此它可以包含连字符、下划线等,但不能包含问号、空格等。
In CakePHP's model, can I $validate a field to require it to not have spaces or other special characters?
Specifically, I want them to type a name for their online folder - so it can have hyphens, underscores, etc, but not question marks, spaces...etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以此作为参考: http://book.cakephp.org/view/1179 /Custom-Validation-Rules
您可以将规则设置为
/^[a-z0-9_\-\.]+$/i
(正则表达式),您只需添加您想要的其他字符。不确定您是否使用正则表达式,但这基本上表示整个字符串
^...$
必须仅包含字母、数字、下划线、连字符(转义)、句点(转义可能不必要,但可能匹配没有它的任何字符)。/i
使其不区分大小写。+
表示您需要其中一个或多个字符才有效。(未经测试,但应该足够简单。)
Using this as a reference: http://book.cakephp.org/view/1179/Custom-Validation-Rules
You could set your rule to
/^[a-z0-9_\-\.]+$/i
(regular expression) you just need to add the additional characters you want.Not sure if you speak regular expressions, but that basically says the whole string
^...$
must contain only letters, numbers, underscores, hyphens (escaped), periods (escape perhaps unnecessary, but might match any character without it)./i
makes it case-insensitive.+
means you need one-or-more of those characters to be valid.(Untested, but it should be straightforward enough.)