Laravel最酷的功能之一是,如果发生验证错误,Laravel会预先填写表单字段.但是,如果页面包含more than one form,并且表单字段包含same name,则Laravel会预填充所有内容forms fields.
例如:
我有一个页面,我有两个表单来创建新用户或任何其他.
<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() }}
Run Code Online (Sandbox Code Playgroud)
调节器
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)

由于我没有填写电子邮件,Laravel将抛出验证错误并预填表格如下:

如何告诉Laravel没有填写第二张表格?
没有Laravel这样做的方法,但您可以使用HTML基本表单数组来使其工作.您需要了解您必须识别您的表单和字段,以便Laravel确切知道数据的来源以及将数据发回的位置.如果您的所有字段都具有相同的名称,那么它怎么可能知道呢?
这是一个可以直接从您的routes.php文件中工作的概念证明.
当我做了这一切,并张贴我用了答案前这里测试Route::get()和Route::post(),让它不必创建一个控制器,只是测试的东西我不会用一个视图.在开发这个时,你必须把这个逻辑放在一个控制器和一个视图中,我认为它们已经完成了.
要按照它的方式进行测试,您只需将浏览器指向以下路径:
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();
});
Run Code Online (Sandbox Code Playgroud)
在这里我们获取数据,选择表单并将其全部传递给验证器:
Route::post('post', function()
{
$input = Input::all();
$rules = [
'name' => 'required',
'email' => 'required'
];
$validation = Validator::make($input['form'][$input['button']], $rules);
return Redirect::back()->withInput();
});
Run Code Online (Sandbox Code Playgroud)
这是你在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() }}
Run Code Online (Sandbox Code Playgroud)
您甚至可以使用循环在刀片中创建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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6412 次 |
| 最近记录: |