Laravel 最酷的功能之一是,如果发生验证错误,Laravel 会预先填写表单字段。但是,如果一个页面包含多个表单
,并且表单字段具有相同的名称
,Laravel 会预填充所有表单字段
。
例如:
我有一个页面,其中有两个表单可以创建新用户或其他任何内容。
<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);
}
}
}
由于我没有填写电子邮件,Laravel 将抛出验证错误并按如下方式预填写表格:
如何告诉 Laravel 不要填写第二个表单?
最佳答案
Laravel 无法做到这一点,但您可以使用 HTML 基本表单数组来实现它。你需要明白你必须识别你的表单和字段,这样 Laravel 才能确切地知道数据来自哪里以及将数据发送回哪里。如果您的所有字段都具有相同的名称,它怎么可能知道?
这是一个概念证明,可以直接从您的 routes.php
文件中运行。
因为我在发布答案之前完成了这一切并在这里进行了测试,所以我使用了 Route::get()
和 Route::post()
,不必创建一个 Controller 和一个 View 只是为了测试我不会使用的东西。在开发这个时,您必须将这个逻辑放在 Controller 和 View 中,我认为它们已经在其中。
要按原样对其进行测试,您只需将浏览器指向以下路由:
http://yourserver/form
当您按下按钮时,它会自动发布路线:
http://yourserver/post
我基本上为所有表单提供了一个数字,并为按钮提供了我们将在 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();
});
在这里我们获取数据,选择表单并将所有数据传递给验证器:
Route::post('post', function()
{
$input = Input::all();
$rules = [
'name' => 'required',
'email' => 'required'
];
$validation = Validator::make($input['form'][$input['button']], $rules);
return Redirect::back()->withInput();
});
这就是你在 Blade View 中使用它的方式,现在使用 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() }}
你甚至可以使用一个循环在 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
关于如果验证失败,Laravel 会预填多个表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20715467/