manix
manix

Reputation: 14747

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".

Upvotes: 0

Views: 2746

Answers (1)

duellsy
duellsy

Reputation: 8577

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.

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.

You will need to ensure that you are redirecting back the form with input like this:

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

If you want to see the magic behind this, see the code here, then here, then finally here

Upvotes: 2

Related Questions