gpt4 book ai didi

php - 如果不需要字段,Laravel 不会验证

转载 作者:可可西里 更新时间:2023-11-01 13:42:27 25 4
gpt4 key购买 nike

我有以下请求验证:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class OwnerEstate extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'firstname' => 'required_if:type,individual',
'secondname'=> 'required_if:type,individual',
'lastname' => 'required_if:type,individual',
'pin' => 'required_if:type,individual|digits:10',

'name' => 'required_if:type,legal-entity',
'eik' => 'required_if:type,legal-entity|digits:9'
];
}
}

当类型不是单独的时,它仍会检查 pin 的 'digits:10' 验证并返回错误。如果 required_if 验证不需要该字段,如何禁用其他验证。 (我正在使用 Laravel 5.5)

最佳答案

digits:10required_if 完全分开,因此它将验证该字段是否为必填。但是,如果您还想允许空值或空值(假设该字段不是必需的),您可以添加规则 nullable

https://laravel.com/docs/5.5/validation#rule-nullable

关于php - 如果不需要字段,Laravel 不会验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46790048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com