Laravel不验证是否不需要字段

The*_*M97 5 php validation laravel

我有以下请求验证:

<?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'
        ];
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Dev*_*von 12

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

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