Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Yii 2中错误验证模式匹配时出现问题_Php_Regex_Yii_Yii2 - Fatal编程技术网

Php 在Yii 2中错误验证模式匹配时出现问题

Php 在Yii 2中错误验证模式匹配时出现问题,php,regex,yii,yii2,Php,Regex,Yii,Yii2,我的rules方法中有一组验证规则,所有错误似乎都有效,但这一个有问题: ['username', 'match', 'pattern' => '/[a-zA-Z0-9_-]+/', 'message' => 'Your username can only contain alphanumeric characters, underscores and dashes.'], 它验证,这是不正确的行为 我在这里做错了什么吗?此模式只检查第一个字符。您需要按如下方式更正它: ['use

我的
rules
方法中有一组验证规则,所有错误似乎都有效,但这一个有问题:

['username', 'match', 'pattern' => '/[a-zA-Z0-9_-]+/', 'message' => 'Your username can only contain alphanumeric characters, underscores and dashes.'],
它验证,这是不正确的行为


我在这里做错了什么吗?

此模式只检查第一个字符。您需要按如下方式更正它:

['username', 'match', 'pattern' => '/^[a-zA-Z0-9_-]+$/', 'message' => 'Your username can only contain alphanumeric characters, underscores and dashes.'],

为了确保Yii的匹配工作正常,您可以通过将
@
(例如)作为第一个字符来测试它。然后,你可以看到,验证是有效的。因此,问题在于您的模式。

上述答案在我的项目中也不起作用。我请求您遵循以下解决方案:

[['userame'],'match', 'pattern' => '/^[a-z]\w*$/i','message' => 'Enter Characters Only'],

以上所有的解决方案都不适合我。这是最好的选择,请尝试一下:

['first_name', 'match', 'pattern' => '/^[a-z][A-Za-z,;\"\\s]+[!?.]$/i'], 

不幸的是,这不起作用。仍然看不到显示的错误消息。@Brett哇,不可能:D,因为我已经在我的机器上首先进行了测试。嗯。。。。。。。也许还有别的事情发生了。我会继续闲逛,看看能发现什么。好吧,我的错。它实际上是在验证,这就是我没有收到错误消息的原因。所以我不知道为什么匹配失败,至少在我的系统上是这样。@Brett-hmmm,你检查过你的场景了吗?也许这一个不包括在您当前的场景中。