【问题标题】:Laravel file upload required validation firing even when file is present即使文件存在,Laravel 文件上传也需要验证触发
【发布时间】:2023-04-01 14:10:01
【问题描述】:

我有一个表单,一个字段用于名为 attachment 的文件,在我的请求中,当另一个名为 requestType 的字段设置为 sick 的值时,该字段的验证要求它存在required_if 验证规则。

我面临的问题是,即使我为有问题的输入字段上传文件,请求类的验证规则仍然会被触发:The attachment field is required when request type is sick.

这是我的代码:

请注意,attachment 字段上的 html required 属性不是导致问题的原因,在页面加载时,它被设置为 disabled,当 requestType 设置为生病时,disabled 属性被删除。

查看

{!! Form::open(['route' => 'employee.request.store', 'class' => 'form-horizontal', 'id' => '', 'files' => 'true']) !!}

            <div class="form-group {{ $errors->first('requestType', 'has-error') }}">
                <label for="" class="col-sm-2 control-label"> {{ Lang::get('employee_request_contractor_create.request_type') }} *</label>
                <div class="col-sm-3">
                    {!! 
                        Form::select('requestType', 
                            ['' => 'Select', 'normal' => 'Normal', 'emergency' => 'Emergency', 'sick' => 'Sick'], 
                            '', 
                            ['class' => 'form-control', 'id' => 'requestType', 'required' => 'required']
                        ) 
                    !!}
                </div>
                {!! $errors->first('requestType', '<label class="col-sm-3 control-label" for="">:message</label>') !!}
            </div>

            <div class="form-group {{ $errors->first('attachment', 'has-error') }}" id="attachmentFormGroup">
                <label for="" class="col-sm-2 control-label"> {{ Lang::get('employee_request_contractor_create.attachment') }} <small>(Sick only)</small> </label>
                <div class="col-sm-3">
                    <input type="file" name="attachment" id="attachment" required="required">
                    <label>(Please provide HR with original copy)</label>
                </div>
                {!! $errors->first('attachment', '<label class="col-sm-3 control-label" for="">:message</label>') !!}
            </div>
    <!-- other form inputs and submit button -->
{!! Form::close() !!}

请求

public function rules()
{
    return [
        'requestType'   => 'required|max:255',
        'attachment'    => 'required_if:requestType,sick|mimes:pdf,jpg,png,gif,jpeg|max:512',
        /* other rules */
    ];

}

如果我删除 required_if:requestType 附件上传就好了,如果我在我的控制器中输出:

if(\Input::hasFile('attachment') echo 'true';

我会看到真的。

当我在控制器存储方法中使用 dd($request) 时,我看到以下内容(相关部分):

+request: ParameterBag {#227 ▼
    #parameters: array:10 [▼
      "_token" => "XkQwP608M5WQ4qtHCYN0dIVETDeqzL0E5ZI99iSf"
      "requestType" => "sick"
      "manager" => "2"
      "dateFrom" => "01-06-2015"
      "dateFromHijri" => "1436-08-14"
      "dateTo" => "02-06-2015"
      "dateToHijri" => "1436-08-15"
      "noOfDays" => "2"
      "location" => "London"
      "contactNumber" => "123456"
    ]
}

还有……

+files: FileBag {#221 ▼
    #parameters: array:1 [▼
      "attachment" => UploadedFile {#27 ▼
        -test: false
        -originalName: "test_doc.pdf"
        -mimeType: "application/pdf"
        -size: 82584
        -error: 0
      }
    ]
  }

是否因为附件未与其他请求属性一起显示而触发规则?

更新:错误消息:

["errors"]=>
  object(Illuminate\Support\ViewErrorBag)#178 (1) {
    ["bags":protected]=>
    array(1) {
      ["default"]=>
      object(Illuminate\Support\MessageBag)#179 (2) {
        ["messages":protected]=>
        array(1) {
          ["attachment"]=>
          array(1) {
            [0]=>
            string(59) "The attachment field is required when request type is sick."
          }
        }
        ["format":protected]=>
        string(8) ":message"
      }
    }
  }

任何帮助将不胜感激。谢谢!

【问题讨论】:

  • $validator-&gt;failed() 的输出是什么?这将为您提供每个验证规则的列表。
  • 查看更新,我在会话的错误包中添加了不使用验证器实例,而是使用中间件进行验证。有什么想法吗?
  • 尝试删除表单打开行中“true”周围的引号。您应该将其设置为布尔值,而不是字符串。
  • @JanWillem 这将如何影响验证者?
  • 这会将 files 参数设置为 true,这意味着将 enctype="multipart/form-data" 添加到表单标签。

标签: php validation laravel laravel-5


【解决方案1】:

由于在requestType 未设置为sick 时禁用输入,因此附件不包含在发送到服务器的POST 请求中。这就是 Laravel 的请求验证文件触发的原因,因为它甚至看不到 POST 数据中的值。要解决这个问题,只需在 attachment 字段的验证字符串中添加 sometimes。代码应如下所示:

public function rules()
{
    return [
        'requestType'   => 'required|max:255',
        'attachment'    => 'sometimes|required_if:requestType,sick|mimes:pdf,jpg,png,gif,jpeg|max:512',
        /* other rules */
    ];

} 

以下是Laravel docs 关于此问题的摘录:

在某些情况下,您可能希望仅当正在验证的数据中存在该字段时才对该字段运行验证检查。要快速完成此操作,请将有时规则添加到您的规则列表中:

$v = Validator::make($data, [

   'email' => 'sometimes|required|email',

]);

在上面的示例中,电子邮件字段只有在 $data 数组中存在时才会被验证。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-30
    • 2018-08-14
    • 2014-06-30
    • 2020-02-17
    • 2017-02-12
    • 2023-03-03
    • 2020-11-29
    • 2016-02-25
    相关资源
    最近更新 更多