简体   繁体   English

CakePHP失败的自定义规则验证(正则表达式)

[英]CakePHP Failing Custom Rule Validation (regex)

I'm following a Lynda.com course for CakePHP 2.4.0 and per the material the following code should work but fails with the latter error message: 我正在关注CakePHP 2.4.0的Lynda.com课程,根据材料,下面的代码应该可以工作,但是后面的错误消息失败了:

    public $validate = array(
        'publication_name' => array(
            'words' => array(
                'rule' => array('custom', '/([\w.-]+ )+[\w+.-]/'),
                'message' => 'The publication name can only contain letters, numbers and spaces.',
            )
        ),
    );

"preg_match(): Delimiter must not be alphanumeric or backslash [CORE/Cake/Model/Validator/CakeValidationRule.php, line 281]". “preg_match():分隔符不能是字母数字或反斜杠[CORE / Cake / Model / Validator / CakeValidationRule.php,第281行]”。

Per my searching, the regex should work as it is using a non-alphanumeric delimiter (/). 根据我的搜索,正则表达式应该工作,因为它使用非字母数字分隔符(/)。 I'm quite perplexed to say the least. 我至少可以说是非常困惑。

The only way I have been able to get it to successfully save the record is by removing the custom validation rule (which is not the desired result for a variety of reasons). 我能够成功保存记录的唯一方法是删除自定义验证规则(由于各种原因,这不是所需的结果)。

Any insight on why this is failing validation? 有关为何验证失败的任何见解? I've diff'd my code against that of the author's exercise files and am not sure what I'm (most likely) over looking... 我已经将我的代码与作者的练习文件区分开来,并且我不确定我(最有可能)在看...

Thank you in advance for your time and insight. 提前感谢您的时间和见解。 I truly appreciate it. 我真的很感激。

Source Code for Model, View and Controllers: http://tny.cz/0a995577 模型,视图和控制器的源代码: http//tny.cz/0a995577

As pointed out in the comments, the regex itself is syntactically correct, so it won't cause such an error. 正如评论中指出的那样,正则表达式本身在语法上是正确的,所以它不会导致这样的错误。 As you figured it was a typo in a different rule causing the problem. 正如您所知,这是导致问题的另一条规则中的拼写错误。

However the rule also won't validate as expected ( http://regex101.com/r/dA0pH9 ), as the regex matches one or more words (including . and - ) separated by a single space, followed by a single word char (again including . and - ). 但是规则也不会按预期验证( http://regex101.com/r/dA0pH9 ),因为正则表达式匹配由单个空格分隔的一个或多个单词(包括.- ),后跟单个单词char (再次包括.- )。 Also it allows to prepend and append arbitrary chars, as it doesn't make use of ^ and $ . 它还允许前置和附加任意字符,因为它不使用^$

To make it match your requirements 使其符合您的要求

one word, multiple words, including numbers and spaces 一个词,多个单词,包括数字和空格

try something like this: 尝试这样的事情:

/^(\w ?)+(?<! )$/

That would match one or more words (which may consist of numbers only, and may be only 1 char in length), separated by a space, not allowing a trailing space. 这将匹配一个或多个单词(可能仅由数字组成,长度可能只有1个字符),由空格分隔,不允许尾随空格。

Though it would also match something like 1 2 3 4 5 ( http://regex101.com/r/kK3jG9/ ), which might not be applicable, so you may want to refine it, for example by requiring at least 1 letter per word, with a minimum length of 3 chars: 虽然它也可以匹配1 2 3 4 5http://regex101.com/r/kK3jG9/ ),这可能不适用,所以你可能想要改进它,例如要求每个至少1个字母单词,最小长度为3个字符:

/^((?=\d*[a-z])\w{3,} ?)+(?<! )$/i

A quick breakdown: 快速分解:

(
  (?=\d*[a-z])  # the word must contain at least one letter
  \w{3,} ?      # a word with at least 3 chars, optionally followed by a space
)+              # one or more times
(?<! )          # no trailing space allowed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM