【问题标题】:Validate time in relation to a checkbox is checked or not?验证与复选框相关的时间是否被选中?
【发布时间】:2019-08-29 07:49:21
【问题描述】:

我有 3 个输入字段(reservationdate、starttime、endtime)和 1 个复选框(holeday)。 如果单击复选框,我不需要开始时间和结束时间。另一方面,开始时间和结束时间是必需的。 我可以做些什么来解决这个任务?

我在 Laravel 中尝试了 required_if 验证功能。但我肯定用错了

// ReservationController store:
$data = $request->validate([
    'userid' => 'required|numeric',
    'vehicleid' => 'required|numeric',
    'budgetid' => 'required|numeric',
    'reservationdate' => 'required|date',
    'starttime' => 'required_if:holeday,on|date_format:H:i|before_or_equal:endtime',
    'endtime' => 'date_format:H:i',
    'holeday' => 'boolean'
]);

index.blade.php(仅复选框)

<div class="input-field col s2">
    <label>
        <input type="checkbox" name="holeday" class="filled-in"  />
        <span>Hole Day</span>
    </label>
</div>

如果选中该复选框,我会收到错误消息“在开斋节时需要开始时间字段。”但在这种情况下,我不需要错误。嘿用户,没关系。我不需要开始时间或结束时间。您点击了当天的活动。

【问题讨论】:

  • 全天休息时不应该需要开始时间和结束时间吗?我认为你的逻辑是颠倒的

标签: laravel validation checkbox time


【解决方案1】:

如果我弄错了,请纠正我:您试图仅在未选中 holeday 时才需要 starttimeendtime

如果我没记错复选框未选中或禁用时,它根本不会发送到服务器,因此您可以验证该字段名称的存在/不存在:

// ReservationController store:
$data = $request->validate([
    'userid' => 'required|numeric',
    'vehicleid' => 'required|numeric',
    'budgetid' => 'required|numeric',
    'reservationdate' => 'required|date',
    'starttime' => 'required_without:holeday|date_format:H:i|before_or_equal:endtime',
    'endtime' => 'required_without:holeday|date_format:H:i',
    'holeday' => 'required_without_all:starttime,endtime|in:on'
]);

注意: boolean 验证仅接受 true1``and "1" 作为 thruthy 值。但是,如果复选框设置为没有 value 属性,您将获得 "on" 值作为默认检查值。

但是,如果您通过所有三个字段及其各自的值,验证仍然会通过,因为 required 并不能确保如果不满足条件,则该字段一定不存在。

您可以考虑到这一点,只需编写逻辑来检查holeday 是否具有“on”值,然后忽略 starttime 和 endtime 值,反之亦然。

否则,您必须使用像这样的自定义验证规则 (未经测试)

Validator::extend('not_present_with', function ($attribute, $value, $parameters, $validator) {
    foreach ($parameters as $parameter) {
        if (! Arr:has($validator->attributes(), $parameter)) {
            return false;
        }
    }

    return ! Arr::has($validator->attributes(), $attribute);
});

然后使用以下验证规则:

// ReservationController store:
$data = $request->validate([
    'userid' => 'required|numeric',
    'vehicleid' => 'required|numeric',
    'budgetid' => 'required|numeric',
    'reservationdate' => 'required|date',
    'starttime' => 'not_present_with:holeday|required_without:holeday|date_format:H:i|before_or_equal:endtime',
    'endtime' => 'not_present_with:holeday|required_without:holeday|date_format:H:i',
    'holeday' => 'not_present_with:starttime,endtime|required_without_all:starttime,endtime|in:on'
]);

【讨论】:

  • 感谢您的回答 mdexp。我认为“holeday”具有误导性。我的意思是一整天的活动(24 小时)。如果用户单击(请求值为“on”)全天复选框,则无需编写开始时间和结束时间。如果我尝试您的第一个代码(不是验证器)并且我检查了复选框和 starttime,endtime empty 我收到错误“... date_format:H:i.
  • 尝试在最后三个规则前添加bail|(在 required_without 规则前)。这告诉 Laravel 停止验证一个字段的剩余规则失败。例如,如果 required_without 失败,则不会验证 date_format
  • 嗨 mdexp。我找到了保释金并尝试了它。没有成功:-((。目前我用Validator检查你的第二个代码。我希望我能和他们一起修复它。我希望你知道我想用这个表单做什么。我不知道这是否是一个好习惯???
猜你喜欢
  • 2017-06-07
  • 1970-01-01
  • 2013-01-25
  • 2019-03-05
  • 2011-04-04
  • 1970-01-01
  • 2019-03-16
  • 2018-09-24
相关资源
最近更新 更多