使用RESTful路由时Laravel 5.1.4验证无法填充旧数据 [英] Laravel 5.1.4 validation fail not populating old data when using RESTful routes
问题描述
我正在尝试建立一个后端管理区域,以将用户添加到我的应用程序中(我不希望用户注册,我希望他们由管理员设置).
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.
如果数据可接受,它将通过POST请求提交到domain.tld/admin/users,并通过UserController@store
存储.
If the data is acceptable it submits via a POST request to domain.tld/admin/users and is stored via UserController@store
.
不满足验证条件时,会将用户发送回表单(domain.tld/admin/users/create),并显示错误消息,但不会使用先前填写的数据填充字段
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.
我正在使用Route::resource('admin/users')
进行RESTful路由创建和干净的路由文件,我发现当对
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.
但是,当对表单所在的创建路径(domain.tld/admin/users/create)发出HTTP POST请求时,验证失败将正确地填充旧数据.
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.
在仍然使用Route::resource('admin/users')
而无需在我的视图中使用{{ old('') }}
帮助器的情况下,有什么方法可以使这项工作正常进行?
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 (非常基本)
<?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
任何帮助将不胜感激.
推荐答案
您要使用类Input
获取Laravel中的旧帖子数据.
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" />
这篇关于使用RESTful路由时Laravel 5.1.4验证无法填充旧数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!