CodeIgniter中的regex_match form_validation生成:消息:preg_match():未找到结束定界符'/' [英] regex_match in CodeIgniter form_validation generates: Message: preg_match(): No ending delimiter '/' found
问题描述
我一直在寻找其他类似的帖子,问题似乎是一个非转义的斜线。但我逃避他们。
I've been looking in other similar posts and the problem seemed to be an unescaped slash. However I'm escaping them.
这是字符串的外观:
23/12/2012
这是我如何声明验证规则:
and this is how I'm declaring the validation rule:
regex_match[/^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)[0-9]{2}$/]
结束分隔符存在,在日期中的斜杠之间用反斜杠转义。我也试过这个略有不同,但我得到相同的错误:
The ending delimiter is there, and the two inbetween slashes for the date are being escaped with a backslash. I've also tried this which is slightly different, but I get the same error:
regex_match[/^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)\d\d$/]
错误在哪里?
strong> EDIT:
根据您的建议,我已尝试使用回调函数。这是声明,它位于正在执行表单验证的控制器类中:
Following your advice, I've tried using a callback function. This is the declaration, which is located within the controller class in which the form validation is being executed:
function mach_date($date) {
/* DEBUG */ echo 'Here I am!'; exit; // execution should stop here displaying the echo
return (bool)preg_match('/^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)\d\d$/', $date);
}
application / config / form_validation.php :
$config = array(
// other validation groups.....,
'articles' => array(
// other validated fields.....,
array(
'field' => 'date_p',
'label' => 'Publishing date',
'rules' => 'callback_match_date'
)
)
);
推荐答案
设置验证规则时, |所以在你的正则表达式中的|是导致验证规则分裂在那些,这是导致错误。请在此处讨论此问题。它似乎是一个限制或错误codeigniter。你可以通过运行一个有和没有|的正则表达式来检查它,看看管道的使用是否会导致错误。如果是这种情况,那么您可能必须通过其他方式regex验证,也许使用回调函数详细 page ,你的函数将使用regex来执行preg_match,当然需要在函数里面,然后返回true / false。
When you set the validation rules you separate them with a | so the |'s in your regex is causing the validation rule to split at those and that is causing the error. Discussion here on the issue. It seems it's a limitation or bug in codeigniter. You can test it out by running a regex with and without |'s and see if the usage of pipes will cause an error. If that is the case then you may have to validate by regex by other means, maybe use a callback function as detailed on this page where your function will do a preg_match using the regex which needs to be inside the function of course and then return true/false.
这篇关于CodeIgniter中的regex_match form_validation生成:消息:preg_match():未找到结束定界符'/'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!