Laravel file upload required validation firing eve

2019-07-22 04:07发布

I have a form, one field is for a file named attachment, in my request the validation for this field requires it to be present when another field named requestType is set to the value of sick by using the required_if validation rule.

The issue I'm facing is that even when I upload a file for the input field in question the validation rule from the request class still gets triggered: The attachment field is required when request type is sick.

Here's my code:

Please note that the html required attribute on the attachment field is not what is causing the issue, on the page load it's set to disabled and when requestType is set to sick the disabled attribute is removed.

View

{!! 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() !!}

Request

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

}

If I remove required_if:requestType the attachment uploads just fine and if I output in my controller:

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

I will see true.

When I dd($request) in my controller store method I see the following (relevant parts):

+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"
    ]
}

and...

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

Is the rule getting fired because attachment is not showing up along with the other request attributes?

Update: Error messages:

["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"
      }
    }
  }

Any help would be much appreciated. Thanks!

0条回答
登录 后发表回答