【问题标题】:Laravel: Recursive Validation Per RuleLaravel:每个规则的递归验证
【发布时间】:2018-05-05 07:05:04
【问题描述】:

我正在开发一个管理员面板,管理员可以在其中为会员申请设置验证自定义验证规则。我知道需要在数据库中设置一个 $attribute => $rules 对。

但是,有一个请求的功能我不太确定如何实现。管理员希望每个 $key => $rule 对递归地并且可选地具有子级 $key => $rule 对,如果父级失败,将执行这些子级。所以最后,每个规则可能有 0 到多个子规则,这些子规则都需要通过才能使父规则通过。

例子:

// Original validation (Assume age = 16, time_at_job = 18 and monthly_income = 3000)
[
    'age' => 'min:21', // Fail, but pass because of subset is all pass
    'time_at_job' => 'min:6' // Pass
    'monthly_income' => 'min:2000' // Pass
]

// If the original age fails and this passes, then age passes and continue to the original 
time_at_job.
[
    'age' => 'min:18, // Fail, but pass because of subset is all pass
    'time_at_job' => 'min:12' // Pass
    'monthly_income' => 'min:2500' // Pass
]

// If the subset age passed and this passes, then the subset age passes, but the subset 
time_at_job and monthly income will need to pass before the original age can pass.
[
    'age' => 'min:16, // Pass
    'time_at_job' => 'min:18' // Pass
    'monthly_income' => 'min:3000' // Pass
]

任何关于从哪里开始的帮助将不胜感激。

【问题讨论】:

    标签: php laravel validation recursion laravel-5


    【解决方案1】:

    我想你可以使用方法调用作为数组值,比如

    public function rules(){
        return [
         'age' => $this->ageChildCheck(),
        ]
    }
    

    然后在验证器中的规则之后:

    public function ageChildCheck(){
        $data = $this->validationData();
        if($data['time_at_job'] > passNumber && $data['monthly_income'] > passNumber2){
            return 'min:0';
        }else{
            return 'min:18';
        }
    }
    

    这应该允许您可变地设置最终的验证规则。如果满足条件则将其设置为 0,如果条件满足则将其设置为 0,如果条件不满足则将其设置为 18。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-09
      • 2016-12-17
      • 2020-06-25
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-13
      相关资源
      最近更新 更多