Laravel不验证是否不需要字段

问题描述 投票:5回答:1

我有以下请求验证:

<?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)

php laravel validation
1个回答
10
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.