-->

Laravel 5 input old is empty

2020-08-21 06:16发布

问题:

My routes is here

Route::get('sign-up', ['as' => 'signUp', 'uses' => 'UserController@signUpGet']);
Route::post('sign-up', ['as' => 'signUpPost', 'uses' => 'UserController@signUpPost']);

Controller

return redirect('signUp')->withInput();

And View

    <form role="form" method="POST" action="{{route('signUpPost')}}"> 
        <input type="text" class="form-control" name="username" value="{{ old('username') }}">
</form>

The {{old()}} function return empty value.
EDIT
I took

NotFoundHttpException in RouteCollection.php line 145:

回答1:

Your problem looks like you are not actually submitting the username in the first place:

<form role="form" method="POST" action="{{route('signUpPost')}}"> 
        <input type="text" class="form-control" name="username" value="{{ old('username') }}">
</form>

There is no 'submit' button inside the form. If you submit outside the form - then the username will not be included.

Add the submit button inside your form - then try again

<form role="form" method="POST" action="{{route('signUpPost')}}"> 
        <input type="text" class="form-control" name="username" value="{{ old('username') }}">
        <input type="submit" value="Submit">
</form>

Edit - also your controller is wrong. It should be this:

 return redirect()->route('signUp')->withInput();


回答2:

All you are missing is to Flash the Input to the session. This is so it's available during the next request.

     $request->flash();

Do that just before calling to View your form.

Source: http://laravel.com/docs/5.1/requests#old-input



回答3:

You can try this: {{ Input::old('username') }}.



回答4:

I realize this isn't the case in this particular situation, but I ran into this problem as well. My problem here was caused by "data-prefill" in the input. Once I removed this, it worked.

Good luck everyone!



回答5:

            <div class="form-group @if($errors->first('username')) has-error @endif">
              <label for="username" class="rtl">Enter user name </label>
              <input type="text" name="username" class="form-control rtl basic-usage" id="username" placeholder="Enter user name" value="{!! old('username') !!}">
              <span class="help-block required">{{$errors->first('username')}}</span>
            </div>

above form field will show old value entered. will show validation error (you have to specify error separately.) have a place holder. bootstrap to look better.(add bootstrap)