Laravel:为日期字段输入旧的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29736532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:23:38  来源:igfitidea点击:

Laravel: Input old for date field

laravellaravel-4eloquentlaravel-5blade

提问by Virgil Joseph Cruz

I want to have my date field remember my old input

我想让我的日期字段记住我的旧输入

Is there any way i can do this?

有什么办法可以做到这一点吗?

Here's my code

这是我的代码

<div class="form-group">
        <label for="date1">Date From: </label>
        <input id="date1" name="date1" type="date"  value="<?php echo date('Y-m-d'); ?>" class="form-control">

    </div>  


<div class="form-group">
        <label for="date2">Date To: </label>
        <input id="date2" name="date2" type="date"  value="<?php echo date('Y-m-d'); ?>" class="form-control">

    </div>  

Please Help. Thank You!

请帮忙。谢谢你!

回答by scrubmx

If you want to have a default but also remember the user provided input in case of any error you could do something like this:

如果你想有一个默认值,但还要记住用户提供的输入以防出现任何错误,你可以这样做:

<input type="date" name="date2" value="{{ old('date2', date('Y-m-d')) }}">

<input type="date" name="date2" value="{{ old('date2', date('Y-m-d')) }}">

Then in your controller when you redirect back make sure to call the ->withInput()method.

然后在您的控制器中,当您重定向回来时,请确保调用该->withInput()方法。

回答by Edgar Orozco

First you need to redirect from your controller "With inputs" something like this:

首先,您需要从控制器“使用输入”重定向,如下所示:

return redirect('some/form/route')->withInput();

Then in your template:

然后在您的模板中:

  Request::old('date_field')

Or in a blade template:

或者在刀片模板中:

  {{ old('date_field') }}

More detail in the "Old Input" section in L5 docs

L5 文档中“旧输入”部分的更多详细信息