简体   繁体   English

使用自定义规则的Laravel数组验证

[英]Laravel array validation with custom rule

In CustomFormRequest i have something like this: CustomFormRequest我有这样的事情:

public function rules(): array
{
    return [
        'events' => ['array'],
        'events.*.type' => ['required'],
        'events.*.data' => [app(SomeRule::class)],
    ];
}

SomeRule : SomeRule

public function passes($attribute, $value): bool
{
    var_dump($attribute);//events.0.data
    var_dump($value);exit;
}

In SomeRule::passes i need to have access to events.X.type (so for events.5.data i need events.5.type ). SomeRule::passes我需要访问events.X.type (所以events.5.data我需要events.5.type )。 Any ideas? 有任何想法吗?

As a user commented in my question Validating array in Laravel using custom rule with additional parameter , you can do it with this code, in your case it would be something like 用户在我的问题中使用自定义规则和附加参数在Laravel中验证数组问题中发表了评论,您可以使用此代码来完成此操作,在这种情况下,这类似于

class SomeRule implements Rule
{
    public function passes($attribute, $value)
    {
        $index = explode('.', $attribute)[1];
        $type = request()->input("events.{$index}.type");

        return someConditional ? true : false;
    }
}

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

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