php - 如果不需要字段,Laravel 不会验证

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

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

最佳答案

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

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

关于php - 如果不需要字段,Laravel 不会验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46790048/

相关文章:

php - OpenSSl + PHP 如何实现

php - 使用带有 php imap 的消息 ID 获取电子邮件?

php - 错误 PHP - 无法分配内存(laravel + aws)

laravel - 如何结合 Laravel 验证 required_if 和 required_without_all 规则?

javascript - 如何在 View 中显示特定的 laravel 验证错误,而不是全部

php - Doctrine\ORM\Mapping\MappingException 实体上列 'username' 的重复定义

PHP 将 float 转换为 int 返回不同的值

javascript - 从 $.load 获取的数组作为选择选项 Laravel Form

jquery - 使用 jquery 验证 &lt;textarea&gt;

JSON 架构 : Element is valid based on another element's value