我有一个表单,一个字段用于名为attachment
的文件,在我的请求中,当另一个名为requestType
的字段设置为{的值时,此字段的验证要求它存在{1}}使用sick
验证规则。
我面临的问题是,即使我上传了相关输入字段的文件,请求类中的验证规则仍会被触发:required_if
这是我的代码:
请注意,The attachment field is required when request type is sick.
字段中的html required
属性不是导致此问题的原因,在页面上将其设置为attachment
以及何时{{ 1}}设置为病态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 */
];
}
我会看到真的。
当我在控制器存储方法中使用dd($ request)时,我看到以下(相关部分):
required_if:requestType
和...
if(\Input::hasFile('attachment') echo 'true';
规则是否因为附件未与其他请求属性一起显示而被解雇?
更新:错误消息:
+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"
]
}
非常感谢任何帮助。谢谢!