简体   繁体   English

在Laravel中将默认值设置为Form:text()

[英]Set default value to Form:text() in Laravel

Well, I am so lost in how to achieve this behavior. 好吧,我很失落如何实现这种行为。 In my view I have this: 在我看来,我有这个:

Form::text('time', Input::old('time'));

So, everything the form validator fails, the view is rendered again with the time input having the value that the user typed in the past... Everything is ok here. 因此,表单验证器失败的所有内容,视图将再次呈现, 时间输入具有用户在过去键入的值...此处一切正常。

What I want to do is to display a default in the time input from the beginning, something like this: 我想要做的是在开头的时间输入中显示默认值,如下所示:

time:  [ now      ]

But if the user change the "now" word and the validator fails, I would like to the display the text submitted by the user instead of "now". 但是如果用户更改“now”字并且验证器失败,我想显示用户提交的文本而不是“now”。

With Laravel 4, you don't have to explicitly tell it to load the old data, it will do this for you since you're using the Form::text macro. 使用Laravel 4,您不必明确告诉它加载旧数据,它将为您执行此操作,因为您使用的是Form::text宏。

So something like this will work for you: 所以这样的事情对你有用:

Form::text('time', 'now');

That will set 'now' as the default, but if there is a value in Input::old('time') it will magically appear as the default. 这会将'now'设置为默认值,但如果Input::old('time')有一个值,它将神奇地显示为默认值。

You will need to ensure that you are redirecting back the form with input like this: 您需要确保with input重定向表单:

return Redirect::route('your.route')->withInput();

If you want to see the magic behind this, see the code here , then here , then finally here 如果你想看到这背后的魔力,请看这里的代码,然后在这里 ,最后在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM