如果验证失败,Laravel 会预填多个表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20715467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel pre-filling multiple forms if validation failed
提问by Anam
One of the coolest Laravel feature is, Laravel pre-filled the form fields if validation error occurred. However, if a page contain more than one form
, and form fields have same name
, Laravel pre-filling all forms fields
.
Laravel 最酷的功能之一是,如果发生验证错误,Laravel 会预先填充表单字段。但是,如果页面包含more than one form
,并且表单字段具有same name
,Laravel 会预填充所有forms fields
.
For example:
例如:
I have a page where i have two forms to create new users or whatever.
我有一个页面,我有两个表单来创建新用户或其他什么。
<h1>Create user1</h2>
{{ Form::open(array('url' => 'foo/bar')) }}
{{ Form::text('name', null) }}
{{ Form::email('email', null) }}
{{ Form::close() }}
</h1>Create user2</h1>
{{ Form::open(array('url' => 'foo/bar')) }}
{{ Form::text('name', null) }}
{{ Form::email('email', null) }}
{{ Form::close() }}
Controller
控制器
class UsersController extends BaseController
{
public function store()
{
$rules = [
'name' => 'required',
'email' => 'required'
];
$validation = Validator::make(Input::all(), $rules);
if ($validation->fails()) {
return Redirect::back()->withInput()->withErrors($validation);
}
}
}
As i didn't fill up the email, Laravel will throw validation error and pre-filling the forms as following:
由于我没有填写电子邮件,Laravel 将抛出验证错误并按如下方式预先填写表单:
How to tell Laravel that do not fill-up the second form?
如何告诉 Laravel 不要填写第二个表格?
回答by Antonio Carlos Ribeiro
There's no Laravel way of doing this, but you can use HTML basic form arrays to make it work. You need to understand that you have to identify your forms and fields so Laravel knows exactly where the data came from and where to send it back to. If all your fields have the same name how could it possibly know?
Laravel 没有这样做的方法,但您可以使用 HTML 基本表单数组来使其工作。你需要明白你必须识别你的表单和字段,这样 Laravel 才能确切地知道数据来自哪里以及将它发送回哪里。如果您的所有字段都具有相同的名称,它怎么可能知道?
This is a proof of concept that will work straight from your routes.php
file.
这是一个概念证明,可以直接从您的routes.php
文件中使用。
As I did it all and tested here before posting the answer I used Route::get()
and Route::post()
, to not have to create a controller and a view just to test something I will not use. While developing this you will have to put this logic in a controller and in a view, where I think they are alredy in.
当我做了这一切,并张贴我用了答案前这里测试Route::get()
和Route::post()
,让它不必创建一个控制器,只是测试的东西我不会用一个视图。在开发这个时,你必须把这个逻辑放在一个控制器和一个视图中,我认为它们已经在那里了。
To test it the way it is, you just have to point your browser to the following routes:
要按原样测试它,您只需将浏览器指向以下路线:
http://yourserver/form
http://yourserver/form
and when you push a button it will automatically POST tho the route:
当您按下按钮时,它会自动发布路线:
http://yourserver/post
http://yourserver/post
I'm basically giving all forms a number and giving the buttons the number that we will usin in Laravel to get the form data and validate it.
我基本上给所有表单一个数字,并给按钮一个数字,我们将在 Laravel 中使用它来获取表单数据并验证它。
Route::get('form', function()
{
return Form::open(array('url' => URL::to('post'))).
Form::text('form[1][name]', null).
Form::email('form[1][email]', null).
'<button type="submit" name="button" value="1">submit</button>'.
Form::close().
Form::open(array('url' => URL::to('post'))).
Form::text('form[2][name]', null).
Form::email('form[2][email]', null).
'<button type="submit" name="button" value="2">submit</button>'.
Form::close();
});
And here we get the data, select the form and pass all of it to the validator:
在这里我们获取数据,选择表单并将其全部传递给验证器:
Route::post('post', function()
{
$input = Input::all();
$rules = [
'name' => 'required',
'email' => 'required'
];
$validation = Validator::make($input['form'][$input['button']], $rules);
return Redirect::back()->withInput();
});
This is how you use it in a Blade view, now using 3 forms instead of 2 and you can have as many forms as you need:
这是您在 Blade 视图中使用它的方式,现在使用 3 个表单而不是 2 个,您可以根据需要拥有任意多个表单:
<h1>Create user1</h2>
{{ Form::open(array('url' => URL::to('post'))) }}
{{ Form::text('form[1][name]', null) }}
{{ Form::email('form[1][email]', null) }}
<button type="submit" name="button" value="1">submit</button>
{{ Form::close() }}
</h1>Create user2</h1>
{{ Form::open(array('url' => URL::to('post'))) }}
{{ Form::text('form[2][name]', null) }}
{{ Form::email('form[2][email]', null) }}
<button type="submit" name="button" value="2">submit</button>
{{ Form::close() }}
</h1>Create user3</h1>
{{ Form::open(array('url' => URL::to('post'))) }}
{{ Form::text('form[3][name]', null) }}
{{ Form::email('form[3][email]', null) }}
<button type="submit" name="button" value="3">submit</button>
{{ Form::close() }}
And you can even use a loop to create 100 forms in blade:
您甚至可以使用循环在 Blade 中创建 100 个表单:
@for ($i=1; $i <= 100; $i++)
User {{$i}}
{{ Form::open(array('url' => URL::to('post'))) }}
{{ Form::text("form[$i][name]", null) }}
{{ Form::email("form[$i][email]", null) }}
<button type="submit" name="button" value="{{$i}}">submit</button>
{{ Form::close() }}
@endfor
回答by Bruno
Use old input with $request->flash().
将旧输入与 $request->flash() 一起使用。