【问题标题】:Laravel Nova Conditional Validation : required_if + exists ruleLaravel Nova 条件验证:required_if + 存在规则
【发布时间】:2020-05-25 19:32:46
【问题描述】:

我正在尝试验证 'parent_code' attr 关于 'level' 字段值

这是我想要实现的目标:

'parent_code' 仅在'level' 为 != 0 时才需要(这部分工作正常)

并且当它被设置时,它必须也存在于表'products''product_code':将使用的列名)

我当前的代码(不能正常工作)

产品资源类

public function fields(Request $request) {
        return [
            ID::make()->sortable(),

            Text::make('Product code', 'product_code')
                ->rules('required')
                ->creationRules('unique:products,product_code')
                ->updateRules('unique:products,product_code,{{resourceId}}')->hideFromIndex(),


            Text::make('Product short name', 'product_short_name')->onlyOnForms(), 


            Textarea::make('Product name', 'product_name')
                ->rules('required')
                ->sortable()
                ->onlyOnForms(),

            Text::make('Parent code', 'parent_code')
                ->rules(['required_if:level,2,4,6', 'exists:products,product_code'])
                ->hideFromIndex(), 

            Select::make('Level', 'level')->options([
                '0' => 'Sector level',
                '2' => 'H2',
                '4' => 'H4',
                '6' => 'H6',
            ])->rules('required')->sortable(),    

        ];
}

创建产品表单

感谢您的帮助。

【问题讨论】:

  • 只有在满足required if 的情况下才需要应用exists 规则吗?
  • 完全正确,仅在需要时满足

标签: php laravel laravel-validation laravel-nova


【解决方案1】:

正确的方法是在验证器实例上使用 sometimes() 方法,但你不能从 Laravel Nova 规则中访问它。

您可以将rules 定义为接收当前传入请求的闭包,并检查该值以动态构建规则数组:

Laravel 集合解决方案

Text::make('Parent code', 'parent_code')
    ->hideFromIndex()
    ->rules(function ($request) {
        // You could also use "->when(...)" instead of "->unless(...)"
        // and invert the condition (first argument)
        return collect(['required_if:level,2,4,6'])
            ->unless($request->level === 0, function ($rules) {
                return $rules->push('exists:products,product_code');
            })
            ->toArray();
    }),

不使用集合的逻辑是一样的,只是使用基本的if语句来动态添加条件:

纯 PHP 数组解决方案

Text::make('Parent code', 'parent_code')
    ->hideFromIndex()
    ->rules(function ($request) {
        return [
            'required_if:level,2,4,6',
            ($request->level !== 0) ? 'exists:products,product_code' : '',
        ];
    }),

或(变体):

Text::make('Parent code', 'parent_code')
    ->hideFromIndex()
    ->rules(function ($request) {
        $rules = ['required_if:level,2,4,6'];

        if ($request->level !== 0) {
            $rules[] = 'exists:products,product_code';
        }

        return $rules;
    }),

【讨论】:

  • 感谢您的帮助和时间,现在我收到“方法 Illuminate\Validation\Validator::validateRequiredIf:level,2,4,6 不存在。”
  • 您使用的是哪个 nova 版本?另外,尝试在unless 调用之后链接->toArray(); 方法。
  • 我使用的是 v2.8.0
  • 这在使用 $request->level === '0' 时非常有效。谢谢
  • 由于您使用的是 Laravel,我向您推荐了一个带有集合的解决方案,但是如果您对普通的 php 数组更满意,请告诉我,我也会添加此解决方案的一个变体,而无需集合。
猜你喜欢
  • 2017-07-08
  • 2019-03-21
  • 1970-01-01
  • 2017-06-13
  • 1970-01-01
  • 2014-04-15
  • 2021-07-22
  • 2014-11-25
  • 2016-10-13
相关资源
最近更新 更多