gpt4 book ai didi

php - 拉维尔 5 : How to validate datetime input from 4 input fields?

转载 作者:可可西里 更新时间:2023-10-31 22:47:52 26 4
gpt4 key购买 nike

我有一个包含截止日期的表单,用户应该像这样在四个输入字段中设置截止日期:

<div class="form-group col-lg-3">
{!! Form::label('year', 'Year', ['class' => 'control-label']) !!}
{!! Form::selectYear('year',$year, $year +1, null , ['class' => 'form-control']) !!}
</div>
<div class="form-group col-lg-3">
{!! Form::label('month', 'Month', ['class' => 'control-label']) !!}
{!! Form::selectRange('month', 1, 12 , null , ['class' => 'form-control']) !!}
</div>
<div class=" form-group col-lg-3">
{!! Form::label('day', 'Day', ['class' => 'control-label']) !!}
{!! Form::selectRange('day', 1, 31 , null , ['class' => 'form-control']) !!}
</div>
<div class=" form-group col-lg-3">
{!! Form::label('hour', 'Hour', ['class' => 'control-label']) !!}
{!! Form::selectRange('hour', 6, 23 , null , ['class' => 'form-control']) !!}
</div>

在 formRequest 中,我将这四个字段编译为截止日期。所以我的 formRequest 是这样的:

public function rules()
{
$this->prepInput();
return [

];
}
public function prepInput(){
$input=$this->all();
...
$input['deadline']=$this->prepDeadline($input['hour'], $input['month'], $input['day'], $input['year']);
...
$this->replace($input);
}


public function prepDeadline($hour,$month,$day, $year){

$time = jDateTime::mktime($hour, 0, 0, $month, $day, $year);
return $deadline = strftime("%Y-%m-%d %H:%M:%S", $time);
}

截止日期是 Jalali 日期时间,我需要检查用户是否选择了有效日期(例如 1394/12/31 不是有效日期)。 jDatetime 包有一个 checkdate 方法,它的工作方式与 php checkdate 完全一样。我在哪里以及如何验证此 formRequest 中的日期并通知用户选择有效日期?事实上,我需要在截止日期传递给 prepInput 之前进行此验证。

最佳答案

Laravel 的 Validator 有一个 date_format 规则,所以在表单请求中设置规则时,你应该能够简单地添加如下内容:

public function rules()
{
$this->prepInput();
return [
'deadline' => 'date_format:Y-m-d H:i:s'
];
}

那么在你看来:

@if ($errors->has('deadline'))
{{ $errors->first('deadline') }}
@endif

您还可以简化 prepInput 方法,只需将年/月/日/小时连接成一个字符串即可创建截止日期;本质上是一样的。

关于php - 拉维尔 5 : How to validate datetime input from 4 input fields?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31647411/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com