首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用laravel验证小时数

使用laravel验证小时数
EN

Stack Overflow用户
提问于 2020-12-05 23:34:20
回答 2查看 46关注 0票数 0

我有这样的帖子:

代码语言:javascript
复制
{
    "store_id": "e422ecfe-4061-4cbd-adc2-364122959dac",
    "opening_hours": [
        {
            "id": "a3489e0f-aaf9-44a7-8af7-4225c25cb40e",
            "store_id": "e422ecfe-4061-4cbd-adc2-364122959dac",
            "day": 1,
            "time_slot1_closed": false,
            "time_slot1_start": "12:34",
            "time_slot1_end": "13:32",
            "time_slot2_closed": false,
            "time_slot2_start": null,
            "time_slot2_end": null,
            "comment": "Velit sint ab temporibus praesentium quo vel."
        }
    ]
}

我想验证"time_slot1_end“是否在"time_slot1_start”之后。要做到这一点,我有以下规则:

代码语言:javascript
复制
return [

    'store_id' => 'required|exists:stores,id',
    'opening_hours.*.day' => 'required|between:1,7',
    'opening_hours.*.time_slot1_start' => 'nullable|date_format:H:i',
    'opening_hours.*.time_slot1_end' => 'nullable|date_format:H:i|after:time_slot1_start',
    ...
];

验证失败。我有这个错误:

代码语言:javascript
复制
{
    "message": "The given data was invalid.",
    "errors": {
        "opening_hours.0.time_slot1_end": [
            "The opening_hours.0.time_slot1_end must be a date after time slot1 start."
        ]
    }
}

我尝试了很多组合,但都没有成功。我的错误是什么?

EN

Stack Overflow用户

回答已采纳

发布于 2020-12-06 03:37:31

您可以编写一个custom validation rule来检查您喜欢的任何值。我们可以对字符串进行简单的比较,因为它们在早上的时间上总是有前导零。

代码语言:javascript
复制
return [
    'store_id' => ['required', 'exists:stores,id'],
    'opening_hours.*.day' => ['required', 'between:1,7'],
    'opening_hours.*.time_slot1_start' => ['nullable', 'date_format:H:i'],
    'opening_hours.*.time_slot1_end' => [
        'nullable',
        'date_format:H:i',
        function ($k, $v, $f) {
            // get the 0 out of opening_hours.0.time_slot_1_end
            $key = explode(".", $k)[1];
            if ($v <= $this->opening_hours[$key]["time_slot1_start"])) {
                $f("Timeslot $key end must be after start time");
            }
        },
    ],
];
票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65158653

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档