Can you also show the code for your view 'admin.users.create'?
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
}
Please or to participate in this conversation.