【问题标题】:Laravel do not validate if field is not requiredLaravel 不验证是否不需要字段
【发布时间】:2018-03-29 03:20:53
【问题描述】:

我得到了以下请求验证:

<?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' 验证 pin 并返回错误。如果 required_if 验证不需要该字段,我如何禁用其他验证。 (我使用的是 Laravel 5.5)

【问题讨论】:

    标签: php laravel validation


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-05-18
      • 1970-01-01
      • 1970-01-01
      • 2017-11-19
      • 2019-02-03
      • 2021-10-01
      • 2023-03-31
      • 2014-06-17
      • 1970-01-01
      相关资源
      最近更新 更多