Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

andrewhaining's avatar

Custom Request not populating old form data on validation fail

I'm trying to build a back end admin area to add users (I don't want users to register I want them to be set up by an admin).

I've created a UsersController.php and a UserCreateRequest.php, code for each can be seen below.

When the validation criteria isn't met, the user is sent back to the form and error messages are shown, but the fields aren't populated with the previously filled out data.

What am I missing? I've set up something similar for a Posts resource that I created but it doesn't work for this Users one.

Any help would be appreciated.

UsersController.php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests\UserCreateRequest;

class UsersController extends Controller
{

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $users = User::all();
        return view('admin.users.index', compact('users'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        return view('admin.users.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(UserCreateRequest $request)
    {
        User::create($request->all());

        return redirect('admin/users');
    }
}

UserCreateRequest.php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class UserCreateRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'first_name' => 'required|max:255',
            'last_name' => 'required|max:255',
            'email' => 'required|confirmed|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ];
    }
}

Create User View

@extends('admin._layouts.master')

@section('title', 'Create a user')

@section('content')

    <form method="post" action="{{ action('Admin\UsersController@store') }}">

        @foreach ($errors->all() as $error)
            <p class="alert alert-danger">{{ $error }}</p>
        @endforeach

        {!! csrf_field() !!}

        <div class="form-group">
            <label for="first_name">First Name</label>
            <input class="form-control" id="first_name" placeholder="First Name" type="text" name="first_name">
        </div>

        <div class="form-group">
            <label for="last_name">Last Name</label>
            <input class="form-control" id="last_name" placeholder="Last Name" type="text" name="last_name">
        </div>

        <div class="form-group">
            <label for="email">Email address</label>
            <input class="form-control" id="email" placeholder="Email" type="email" name="email">
        </div>

        <div class="form-group">
            <label for="email_confirmation">Confirm email address</label>
            <input class="form-control" id="email_confirmation" placeholder="Email" type="email" name="email_confirmation">
        </div>

        <div class="form-group">
            <label for="password">Password</label>
            <input class="form-control" id="password" placeholder="Password" type="password" name="password">
        </div>

        <div class="form-group">
            <label for="password_confirmation">Confirm Password</label>
            <input class="form-control" id="password_confirmation" placeholder="Password" type="password" name="password_confirmation">
        </div>

        <button class="btn btn-default" type="submit">Submit</button>
    </form>

@endsection

The data is being held in the session as shown in the dump below...

#session: Store {#97 ▼
    #id: "ebf1da3e17a38616ebe166021e0f9124252f2e44"
    #name: "laravel_session"
    #attributes: array:6 [▼
      "_token" => "mCZhbjqumIn96KsLlixeTSZrPrKcoe9nOVfquQf5"
      "flash" => array:2 [▶]
      "_previous" => array:1 [▶]
      "login_82e5d2c56bdd0811318f0cf078b78bfc" => 6
      "_old_input" => array:5 [▼
        "_token" => "mCZhbjqumIn96KsLlixeTSZrPrKcoe9nOVfquQf5"
        "first_name" => "andrew"
        "last_name" => "haining"
        "email" => "test@test.com"
        "email_confirmation" => "test@test.com"
      ]
      "errors" => ViewErrorBag {#122 ▶}
    ]
    #bags: []
    #metaBag: MetadataBag {#114 ▶}
    #bagData: array:1 [▶]
    #handler: FileSessionHandler {#113 ▶}
    #started: true
  }
0 likes
13 replies
aliqsyed's avatar

Can you also show the code for your view 'admin.users.create'?

1 like
helmerdavila's avatar

Store with fake data and dump

dd( $request->all() );

compare with the rules, maybe you cand find the error.

Question: Confirmed in password? you make two inputs.

    <div class="form-group">
                    <label class="col-sm-3 control-label">Password</label>
                    <div class="col-sm-6">
                        <input type="password" name="password" minlength="6" class="form-control" required>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">Confirm Password</label>
                    <div class="col-sm-6">
                        <input type="password" name="password_confirmation" minlength="6" class="form-control" >
                    </div>
                </div>

In your UserCreateRequest, like this

'password' => 'required|alpha_num|min:6|confirmed',
'password_confirmation' => 'required|alpha_num|min:6',
aliqsyed's avatar

you need to use the old() function in your view to get access to the previously filled out values.

<input class="form-control" id="first_name" placeholder="First Name" type="text" name="first_name" value="{{old('first_name'}}">

Note that I added this part to your input
value="{{old('first_name)'}}"

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

andrewhaining's avatar

@aliqsyed you don't need to use the old() function when using a validation request. I haven't needed to on any other resources I've created with custom requests.

@helmerdavila my confirmation validation works correctly and is set up as per the documentation...

The field under validation must have a matching field of foo_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input.

andrewhaining's avatar

@helmerdavila That is in my original code, been stripped out here for some reason, that isn't the issue, errors are displaying correctly, it's the old form data that isn't being repopulated when validation has failed.

andrewhaining's avatar

The old helper will work, but I shouldn't need to use the old helper, it should just work with the custom request as it does with all of my others.

I want to avoid having unnecessary code in my views to fix something that should work out of the box.

andrewhaining's avatar

I figured out the problem...

It works only when the form submission is posted to itself (see example routes below)

Route::get('admin/users/create', 'Admin\UsersController@create');
Route::post('admin/users/create', 'Admin\UsersController@store');

If you use the RESTful method, posting to the parent route it doesn't (again, see below)

Route::get('admin/users/create', 'Admin\UsersController@create');
Route::post('admin/users', 'Admin\UsersController@store');

This also applies if the Route::resource() method is used.

Not sure if there is a solution that is cleaner than using the {{ old('field_name') }} helper.

Any ideas?

erin5995's avatar

I ended up using : $first_name = old('first_name', $request->first_name);

rwakos's avatar

Version 5.4 and still the same error, tried setting the routes to the exact route, but it failed... It works with the "old" function...

Please or to participate in this conversation.