我正在使用Laravel。我想验证一个复杂的请求输入,但当我使用方法requiredIf
时,会启动一个致命的异常,因此我无法读取验证器的所有错误。例如,如果输入$request->assistanceRequest['source']
不存在,则验证不起作用,因为引发了以下异常:
Method IlluminateValidationRulesRequiredIf::__toString() must not throw an exception, caught ErrorException: Undefined index: source
这是控制器中的代码:
$rules = array(
'assistanceRequest.requestType' => 'required|exists:request_types,variable',
'assistanceRequest.deliveryMode' => 'required|in:NORMAL,FAST',
'assistanceRequest.creationDate' => ['required', 'regex:@^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|1[0-9]|0[0-9]):[0-5][0-9]:[0-9]{2}$@'],
'assistanceRequest.source' => 'required',
'assistanceRequest.language' => ['required', 'regex:@^[A-Z]{2}$@'],
'assistanceRequest.country' => ['required', 'regex:@^[A-Z]{2}$@'],
'customer.customerId' => 'required|integer',
'customer.login' => 'required',
'customer.companyName' => 'required',
'customer.phoneNumber' => 'required',
'customer.email' => ['required', 'regex:&^S+@S+.S+$&'],
'vehicle.plateNumber' => 'required',
'vehicle.Country' => ['required', 'regex:@^[A-Z]{2}$@'],
'vehicle.frenchRegistration' => '',
'vehicle.vin' => 'required|min:17|max:17',
'vehicle.variationId' => 'required|integer|max:9999999',
'vehicle.makeLabel' => 'required',
'vehicle.modelLabel' => 'required',
'vehicle.variationLabel' => 'required',
'vehicle.bodyLabel' => Rule::requiredIf(function () use ($request) {
return $request->assistanceRequest['source'] == "ATD"; //example problem
}),
'vehicle.releaseDate' => 'required|date_format:Y-m-d',
'vehicle.specificitiesOptions' => '',
'vehicle.mileage' => Rule::requiredIf(function () use ($request) {
return $request->assistanceRequest['requestType'] !== "SERVICE_PLAN";
}),
'vehicle.distanceUnit' => Rule::requiredIf(function () use ($request) {
return $request->assistanceRequest['requestType'] !== "SERVICE_PLAN";
}),
'requestInformations.category' => Rule::requiredIf(function () use ($request) {
return ($request->assistanceRequest['requestType'] == "TECHNICAL_DATA" or $request->assistanceRequest['requestType'] == "REPAIR_METHOD" or $request->assistanceRequest['requestType'] == "WIRE_DIAGRAM") and $request->assistanceRequest['source'] == 'ATD'; //chiedere e modificare 5
}),
'requestInformations.subCategory' => Rule::requiredIf(function () use ($request) {
return ($request->assistanceRequest['requestType'] == "TECHNICAL_DATA" or $request->assistanceRequest['requestType'] == "REPAIR_METHOD" or $request->assistanceRequest['requestType'] == "WIRE_DIAGRAM") and $request->assistanceRequest['source'] == 'ATD'; //chiedere e modificare 5
}),
'requestInformations.informationCode' => Rule::requiredIf(function () use ($request) {
return ($request->assistanceRequest['requestType'] == "TECHNICAL_DATA" or $request->assistanceRequest['requestType'] == "REPAIR_METHOD" or $request->assistanceRequest['requestType'] == "WIRE_DIAGRAM") and $request->assistanceRequest['source'] == 'ATD'; //chiedere e modificare 5
}),
'requestInformations.dataLabel' => Rule::requiredIf(function () use ($request) {
return ($request->assistanceRequest['requestType'] == "TECHNICAL_DATA" or $request->assistanceRequest['requestType'] == "REPAIR_METHOD" or $request->assistanceRequest['requestType'] == "WIRE_DIAGRAM") and $request->assistanceRequest['source'] == 'ATD'; //chiedere e modificare 5
}),
'requestInformations.cuCode' => Rule::requiredIf(function () use ($request) {
return ($request->assistanceRequest['requestType'] == "TECHNICAL_DATA" or $request->assistanceRequest['requestType'] == "REPAIR_METHOD" or $request->assistanceRequest['requestType'] == "WIRE_DIAGRAM") and $request->assistanceRequest['source'] == 'ATD'; //chiedere e modificare 5
}),
'requestInformations.text' => 'required',
'requestInformations.errorCodes' => Rule::requiredIf(function () use ($request) {
return $request->assistanceRequest['requestType'] == "BREAKDOWN" or $request->assistanceRequest['requestType'] == "ERROR_CODE";
}),
'requestInformations.troubles' => Rule::requiredIf(function () use ($request) {
return $request->assistanceRequest['requestType'] == "BREAKDOWN" or $request->assistanceRequest['requestType'] == "ERROR_CODE";
}),
'requestInformations.controls' => Rule::requiredIf(function () use ($request) {
return $request->assistanceRequest['requestType'] == "BREAKDOWN" or $request->assistanceRequest['requestType'] == "ERROR_CODE";
}),
);
$validator = Validator::make( $request->all(), $rules);
能帮忙吗?这是解决那个问题的简单方法吗?
在Rule::requiredIf(function (){})
规则中,您使用了$request->assistanceRequest['source']
变量,错误显示$request->assistanceRequest
数组中不存在source
键。
解决方案1:使用??
运算符:
...
'vehicle.bodyLabel' => Rule::requiredIf(function () use ($request) {
return $request->assistanceRequest['source'] ?? null == "ATD"; //example problem
}),
...
解决方案2:使用isset()
功能:
...
'vehicle.bodyLabel' => Rule::requiredIf(function () use ($request) {
if(!isset($request->assistanceRequest['source'])) return false;
return $request->assistanceRequest['source'] ?? null == "ATD"; //example problem
}),
...
解决方案3:使用request()
助手:
...
'vehicle.bodyLabel' => Rule::requiredIf(function () use ($request) {
return request('assistanceRequest.source', null) == "ATD"; //example problem
}),
...
注意,您已经在多个验证规则中使用了它,并且您必须更改所有的