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