Laravel不验证是否不需要字段

时间:2017-10-17 12:22:12

标签: php laravel validation

我已获得以下请求验证:

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

如果类型不是个人类型,它仍会检查&#39;数字:10&#39; 验证引脚并返回错误。如果required_if验证不需要该字段,如何禁用其他验证。 (我使用的是Laravel 5.5)

1 个答案:

答案 0 :(得分:10)

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

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