问题
I'm trying to build a back end admin area to add users to my application (I don't want users to register I want them to be set up by an admin).
I've created the necessary controller and request.
If the data is acceptable it submits via a POST request to domain.tld/admin/users and is stored via UserController@store
.
When the validation criteria isn't met, the user is sent back to the form (domain.tld/admin/users/create) and error messages are shown, but the fields aren't populated with the previously filled out data.
I'm using Route::resource('admin/users')
for RESTful route creation and a clean routes file, I've found that when the HTTP POST request is made to the parent route (domain.tld/admin/users) as generated by Route::resource()
, the FormRequest doesn't populate the old data.
But when the HTTP POST request is made to the create route where the form is situated (domain.tld/admin/users/create), the validation fail populates the old data correctly.
Is there any way to make this work as it should whilst still using Route::resource('admin/users')
without having to use the {{ old('') }}
helper in my views?
UsersController (very basic)
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use App\Http\Requests\Admin\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()
{
$users = User::all();
return view('admin.users.create');
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
* @return Response
*/
public function store(UserCreateRequest $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param Request $request
* @param int $id
* @return Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
//
}
}
UserCreateRequest
<?php
namespace App\Http\Requests\Admin;
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.blade.php
@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
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
{!! 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
Any help would be greatly appreciated.
回答1:
you want to use the class Input
to get old post data in Laravel.
<input type="text" class="form-control" value="{{Input::old('name')}}" name="name" placeholder="Enter name" required="required" />
来源:https://stackoverflow.com/questions/32107118/laravel-5-1-4-validation-fail-not-populating-old-data-when-using-restful-routes