Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

单字段多规则中,正则的值,能否用变量? #41

Open
ZhangChengLin opened this issue Sep 9, 2021 · 7 comments
Open

单字段多规则中,正则的值,能否用变量? #41

ZhangChengLin opened this issue Sep 9, 2021 · 7 comments
Assignees
Labels

Comments

@ZhangChengLin
Copy link
Contributor

$MultipleRules = [
    ['captchaCode', 'fixedSize:4', 'msg' => $msg],
    ['username', 'required|string:3,15|regexp:{$regexp["username"]}', 'msg' => $msg],
//    ['username', 'required|string:3,15|regexp:^[a-zA-Z]\w{4,19}$', 'msg' => $msg],
    ['password', 'required|string:8,20|regexp:{$regexp["password"]}', 'msg' => $msg],
];

比如说 {$regexp["username"]}

不能这么写吗?

@ZhangChengLin
Copy link
Contributor Author

不好意思 刚发出来 想到了 应该用双括号

@inhere
Copy link
Owner

inhere commented Sep 10, 2021

'required|string:8,20|regexp:{$regexp["password"]}'

外层换为 双引号 PHP才会解析里面的变量

"required|string:8,20|regexp:{$regexp['password']}"

@inhere inhere self-assigned this Sep 10, 2021
@ZhangChengLin
Copy link
Contributor Author

'required|string:8,20|regexp:{$regexp["password"]}'

外层换为 双引号 PHP才会解析里面的变量

"required|string:8,20|regexp:{$regexp['password']}"

实际中没有进行正则验证,就通过了??
请测试。

@inhere
Copy link
Owner

inhere commented Sep 11, 2021

有测试示例吗

@ZhangChengLin
Copy link
Contributor Author

ZhangChengLin commented Sep 11, 2021

有测试示例吗


<?php

require_once dirname(__DIR__, 4) . '/vendor/autoload.php';

use Inhere\Validate\Locale\LocaleZhCN;

LocaleZhCN::register();

$rules = [
    ['captchaCode,username,password', 'required'],
];

$regexp = [
    'captchaCode' => '',
    'username'    => '/^[a-zA-Z]\w{3,15}$/',
    'password'    => '/^\S*(?=\S{8,})(?=\S*\d)(?=\S*[a-zA-Z])(?=\S*[`.~!@#$%^&*? ])\S*$/',
];

$MultipleRules = [
    ['username', "required|string:3,15|regexp:{$regexp['username']}"],
    ['captchaCode', 'required|fixedSize:4'],
    ['password', "required|string:8,20|regexp:{$regexp["password"]}"],
];

$translates = [
    'username'    => '用户名',
    'password'    => '密码',
    'captchaCode' => '验证码',
];

//$safePost = Inhere\Validate\Validation::check($_POST, $rules, $translates);
$safePost = Inhere\Validate\FieldValidation::check($_POST, $MultipleRules, $translates);


if ($safePost->isFail()) {

    var_dump($safePost->getErrors());
    echo "\r\n";
    echo $safePost->firstError();
    echo "\r\n";
    echo json_encode($safePost->getErrors(), JSON_UNESCAPED_UNICODE);
    echo "\r\n";
    exit('出错结束');
}

echo "\r\n";

//$safePostData = $safePost->all();
$safePostData = $safePost->getSafeData();
//var_dump($safePostData);

exit('完全完成');


这是一部分,应该可以吧。

@ZhangChengLin
Copy link
Contributor Author

ZhangChengLin commented Sep 12, 2021

@inhere

建了个分支,请帮忙查看,在运行时候,会提示报错,但没有影响运行。
报错信息如下

[12-Sep-2021 23:36:03 Asia/Shanghai] PHP Warning:  filter_var(): No ending delimiter '/' found in D:\PhpstormProjects\GitHub\php-validate\vendor\inhere\php-validate\src\Validators.php on line 715
[12-Sep-2021 23:36:03 Asia/Shanghai] PHP Warning:  filter_var(): No ending delimiter '/' found in D:\PhpstormProjects\GitHub\php-validate\vendor\inhere\php-validate\src\Validators.php on line 715

新建的测试分支地址如下
https://github.com/ZhangChengLin/php-validate/tree/example

里面有具体的代码和有关注释信息,请查看,盼回复啊。

具体可能和正则验证有关。

@inhere
Copy link
Owner

inhere commented Sep 13, 2021

hao

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants