Laravel 不验证字段是否为必填字段

2022-08-31 00:34:38

我有以下请求验证:

<?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'
        ];
    }
}

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


答案 1

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

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


答案 2

推荐