基于Laravel数组的日期验证after_or_equal多个日期?



我正在使用laravel-excel包导入excel文件到集合。我试图验证日期字段reporting_date,使其为after_or_equal,另外两个日期字段registered_dateanalysis_date。只有一个after_or_equal日期字段,所有这些都工作得很好。我正在动态地填充规则和自定义消息数组。规则是正确应用的,但是我有问题,用自定义消息覆盖这个特定验证的默认消息,而不是显示默认消息。

public function collection(Collection $rows)
{
$data = $rows->toArray();
$rules = [Some static rules defined];
$messages = [];


foreach ($data as $key => $val){
/*Adding [complex/]conditional rules and messages.*/
...
...
if(!empty($val['reporting_date'])){
$rules = array_merge($rules, [$key.'.reporting_date' => ['sometimes', 'nullable', 'date', 'after_or_equal:'.$key.'.registered_date,', 'after_or_equal:'.$key.'.analysis_date']]);
$messages["$key.reporting_date.date"] = "Error on row: <strong>".($key+2)."</strong>. The reporting_date <strong>".(Arr::exists($val, "reporting_date")?$val['reporting_date']:"").
"</strong> is not a valid date.";

/****These messages are not overriding the default message.****/  
$messages["$key.reporting_date.after_or_equal.$key.registered_date"] = "Error on row: <strong>".($key+2)."</strong>. The reporting_date <strong>".(Arr::exists($val, "reporting_date")?$val['reporting_date']:"").
"</strong> must be a date after or equal to registered_date <strong>".(Arr::exists($val, "registered_date")?$val['registered_date']:"").
"</strong>.";

$messages["$key.reporting_date.after_or_equal.$key.analysis_date"] = "Error on row: <strong>".($key+2)."</strong>. The reporting_date <strong>".(Arr::exists($val, "reporting_date")?$val['reporting_date']:"").
"</strong> must be a date after or equal to analysis_date <strong>".(Arr::exists($val, "analysis_date")?$val['analysis_date']:"").
"</strong>.";
...
...


}
}  
}

我得到的消息是

The 0.reporting_date must be a date after or equal to 0.registered_date.
The 0.reporting_date must be a date after or equal to 0.analysis_date.

验证规则:

"0.reporting_date" => array:5 [▼
0 => "sometimes"
1 => "nullable"
2 => "date"
3 => "after_or_equal:0.registered_date,"
4 => "after_or_equal:0.analysis_date"
]

我怀疑我没有将消息分配给正确的数组键,因此我没有看到自定义消息。现在它是这样分配的,我希望显示这个消息。

"0.reporting_date.after_or_equal.0.registered_date" => "Error on row: 2. The reporting_date 2020-09-08 must be a date after or equal to registered_date 2020-09-12."
"0.reporting_date.after_or_equal.0.analysis_date" => "Error on row: 2. The reporting_date 2020-09-08 must be a date after or equal to analysis_date 2020-09-14."

还在调试错误信息。

dd($validator->errors()); 
0.reporting_date" => array:2 [▼
0 => "The 0.reporting_date must be a date after or equal to 0.registered_date."
1 => "The 0.reporting_date must be a date after or equal to 0.analysis_date."
]

若要仅为特定属性指定自定义错误消息,请先指定该属性的名称,然后遵循以下规则:

field.rule => message

$messages = [
'email.required' => 'We need to know your email address!',
];

所以我必须检查0.reporting_date是否为after_or_equal0.registered_date0.analysis_date。我设置了如下规则:

"0.reporting_date" => array:5 [▼
0 => "sometimes"
1 => "nullable"
2 => "date"
3 => "after_or_equal:0.registered_date,"
4 => "after_or_equal:0.analysis_date"
]

after_or_equal的其中一个验证失败时,我可以覆盖像

这样的消息
0.reporting_date.after_or_equal => "custom message"

但是当after_or_equal:0.registered_dateafter_or_equal:0.analysis_date都失败时,我认为可以像下面这样为个别情况编写自定义消息,这将是理想的。

"0.reporting_date.after_or_equal.0.registered_date" => "custom message1"
"0.reporting_date.after_or_equal.0.analysis_date" => "custom message2"

但这不起作用,我不得不做一点hack,手动比较日期,然后附加两个消息,解决了这个问题。

if(!(Carbon::parse(0.reporting_date)->gte(Carbon::parse(0.registered_date)) && Carbon::parse(0.reporting_date)->gte(Carbon::parse(0.analysis_date)))){
0.reporting_date.after_or_equal => "custom message1"."custom message2"
}

我想看看是否有其他更好的方法。

相关内容

  • 没有找到相关文章