如何在laravel中验证日期

时间:2017-12-07 11:08:13

标签: php laravel validation laravel-5 laravel-5.3

在验证方法中,我有以下内容:

$this->validate($request, ['test_date' => 'required|unique:test_db']);

test_date中的test_db存储为unix时间。 提交表单后,我会在$request->get('test_date')

中获得mm/dd/Y

唯一验证不起作用,因为我在mm/dd/Y中得到的日期与db(unix时间)不同。

如何在此验证中使其独一无二?

3 个答案:

答案 0 :(得分:3)

你可以试试这个:

======================================================================
Processing event files... (this can take a few minutes)
======================================================================

Found event files in:
summaries/hseq2seq_300_512_1024_1024
summaries/hseq2seq_300_1024_2048_2048
summaries/hseq2seq_300_256_512_512
summaries/hseq2seq_300_128_256_256

These tags are in summaries/hseq2seq_300_512_1024_1024:
audio -
histograms -
images -
scalars
   agent_rewards_summary
   decoder_loss_summary
tensor -
======================================================================

Event statistics for summaries/hseq2seq_300_512_1024_1024:
audio -
graph
   first_step           0
   last_step            0
   max_step             0
   min_step             0
   num_steps            1
   outoforder_steps     []
histograms -
images -
scalars
   first_step           0
   last_step            999999
   max_step             999999
   min_step             0
   num_steps            1000000
   outoforder_steps     []
sessionlog:checkpoint -
sessionlog:start -
sessionlog:stop -
tensor -
======================================================================

These tags are in summaries/hseq2seq_300_1024_2048_2048:
audio -
histograms -
images -
scalars
   agent_rewards_summary
   decoder_loss_summary
tensor -
======================================================================

Event statistics for summaries/hseq2seq_300_1024_2048_2048:
audio -
graph
   first_step           0
   last_step            0
   max_step             0
   min_step             0
   num_steps            1
   outoforder_steps     []
histograms -
images -
scalars
   first_step           0
   last_step            999999
   max_step             999999
   min_step             0
   num_steps            1000000
   outoforder_steps     []
sessionlog:checkpoint -
sessionlog:start -
sessionlog:stop -
tensor -
======================================================================

These tags are in summaries/hseq2seq_300_256_512_512:
audio -
histograms -
images -
scalars
   agent_rewards_summary
   decoder_loss_summary
tensor -
======================================================================

Event statistics for summaries/hseq2seq_300_256_512_512:
audio -
graph
   first_step           0
   last_step            0
   max_step             0
   min_step             0
   num_steps            1
   outoforder_steps     []
histograms -
images -
scalars
   first_step           0
   last_step            999999
   max_step             999999
   min_step             0
   num_steps            1000000
   outoforder_steps     []
sessionlog:checkpoint -
sessionlog:start -
sessionlog:stop -
tensor -
======================================================================

These tags are in summaries/hseq2seq_300_128_256_256:
audio -
histograms -
images -
scalars
   agent_rewards_summary
   decoder_loss_summary
tensor -
======================================================================

Event statistics for summaries/hseq2seq_300_128_256_256:
audio -
graph
   first_step           0
   last_step            0
   max_step             0
   min_step             0
   num_steps            1
   outoforder_steps     []
histograms -
images -
scalars
   first_step           0
   last_step            999999
   max_step             999999
   min_step             0
   num_steps            1000000
   outoforder_steps     []
sessionlog:checkpoint -
sessionlog:start -
sessionlog:stop -
tensor -
======================================================================
```

在请求中添加字段:Laravel is there a way to add values to a request array

更新: 您可以使用offsetSet方法更新现有字段:

//get date from request
$date = $request->get('test_date');

//add the converted date to request
$request->request->add(['new_date_field'=> DateTime::createFromFormat('m/d/Y', $date)->getTimestamp()]);

//validate the updated date field
$this->validate($request, ['new_date_field' => 'required|unique:test_db']);

更新请求字段: Modify request field value before saving in Laravel 5

答案 1 :(得分:0)

我认为如果你改变日期格式,那么一切都会好的,所以试试这个:

$date = $request->get('test_date');

$newDate = date('d-m-Y', strtotime($date ));

在验证中使用此$newDate

答案 2 :(得分:0)

Laravel提供名为Accessors和mutators的功能。

Accessor和mutators允许您在模型实例上检索或设置Eloquent属性值时设置其格式。

有关详细信息,请访问:https://laravel.com/docs/5.5/eloquent-mutators#accessors-and-mutators

希望这会对你有所帮助。