Skip to content

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

Open

Description

@ZhangChengLin
$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"]}

不能这么写吗?

Activity

  1. ZhangChengLin commented on Sep 9, 2021

    @ZhangChengLin
    ContributorAuthor

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

  2. inhere commented on Sep 10, 2021

    @inhere
    Owner

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

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

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

  3. self-assigned this
    on Sep 10, 2021
  4. ZhangChengLin commented on Sep 10, 2021

    @ZhangChengLin
    ContributorAuthor

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

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

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

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

  5. inhere commented on Sep 11, 2021

    @inhere
    Owner

    有测试示例吗

  6. ZhangChengLin commented on Sep 11, 2021

    @ZhangChengLin
    ContributorAuthor

    有测试示例吗

    
    <?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('完全完成');
    
    
    

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

  7. ZhangChengLin commented on Sep 12, 2021

    @ZhangChengLin
    ContributorAuthor

    @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

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

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

  8. inhere commented on Sep 13, 2021

    @inhere
    Owner

    hao

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions