【发布时间】:2020-08-04 11:19:58
【问题描述】:
以下验证未通过 start_date 和 end_date 字段。
ErrorException: strtotime() 期望参数 1 为字符串、数组 给定
public function rules()
{
return [
'location_id.*' => 'required_without_all:user_id,service_id|integer|gt:0',
'service_id.*' => 'required_without_all:user_id,location_id|integer|gt:0',
'user_id.*' => 'required_without_all:location_id,service_id|integer|gt:0',
'start_date.*' => 'required|date|after_or_equal:now',
'end_date.*' => 'nullable|date|after_or_equal:start_date',
'day_name.*' => 'nullable|integer|between:1,7',
'start_time.*' => 'required|date_format:H:i|before:end_time',
'end_time.*' => 'required|date_format:H:i|after:start_time',
'created_by_user_id.*' => 'required|integer|gt:0',
];
}
protected function prepareForValidation()
{
$row = count($this->day_name);
$this->merge([
'location_id' => array_fill(0, $row, $this->location_id),
'service_id' => array_fill(0, $row, $this->service_id),
'user_id' => array_fill(0, $row, $this->user_id),
'start_date' => array_fill(0, $row, Carbon::parse($this->start_date)),
'end_date' => array_fill(0, $row, Carbon::parse($this->end_date)),
'day_name' => array_fill(0, $row, $this->day_name),
'start_time' => array_fill(0, $row, $this->start_time),
'end_time' => array_fill(0, $row, $this->end_time),
'created_by_user_id' => array_fill(0, 3, $this->created_by_user_id),
]);
start_date 是一个碳实例,如下所示:
dd($this->start_date)); // In the start of public function rules()
array:3 [
0 => Carbon\Carbon @1587484783^ {#1470
#constructedObjectId: "000000006789d240000000000f12e56f"
#localMonthsOverflow: null
#localYearsOverflow: null
#localStrictModeEnabled: null
#localHumanDiffOptions: null
#localToStringFormat: null
#localSerializer: null
#localMacros: null
#localGenericMacros: null
#localFormatFunction: null
#localTranslator: null
#dumpProperties: array:3 [
0 => "date"
1 => "timezone_type"
2 => "timezone"
]
#dumpLocale: null
date: 2020-04-21 15:59:43.430962 UTC (+00:00)
timezone: "UTC"
}
1 => Carbon\Carbon @1587484783^ {#1470}
2 => Carbon\Carbon @1587484783^ {#1470}
]
所有字段都属于同一个表。有些是非数组,有些是数组输入。因此将它们全部转换为数组以进行多条记录插入。
【问题讨论】:
-
您是否尝试在每个数组的相同索引处验证 start_date 到 end_date?像这样:
start_date[3] < end_date[3]? -
@RobertKujawa 我通过排除 end_date 尝试了 start_date 的整个过程。但同样的错误
-
Carbon::parse()出现错误。你看,你给了一个数组进行碳解析,它接受一个字符串。
标签: arrays laravel validation