php - Laravel file upload required validation firing even when file is present -


i have form, 1 field file named attachment, in request validation field requires present when field named requesttype set value of sick using required_if validation rule.

the issue i'm facing when upload file input field in question validation rule request class still gets triggered: the attachment field required when request type sick.

here's code:

please note html required attribute on attachment field not causing issue, on page load it's set disabled , when requesttype set sick disabled attribute 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 original copy)</label>                 </div>                 {!! $errors->first('attachment', '<label class="col-sm-3 control-label" for="">:message</label>') !!}             </div>     <!-- other form inputs , 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 remove required_if:requesttype attachment uploads fine , if output in controller:

if(\input::hasfile('attachment') echo 'true'; 

i see true.

when dd($request) in controller store method see 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 rule getting fired because attachment not showing along 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 required when request type sick."           }         }         ["format":protected]=>         string(8) ":message"       }     }   } 

any appreciated. thanks!


Comments