如果验证失败,Laravel会预先填写多种表格 [英] Laravel pre-filling multiple forms if validation failed

查看:91
本文介绍了如果验证失败,Laravel会预先填写多种表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Laravel最酷的功能之一是,如果发生验证错误,Laravel会预先填写表单字段.但是,如果页面包含more than one form,并且表单字段具有same name,则Laravel会预先填充所有forms fields.

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.

例如:

我在一个页面上有两种创建新用户或其他任何形式的表单.

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() }}

控制器

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将抛出验证错误并按以下方式预先填写表格:

As i didn't fill up the email, Laravel will throw validation error and pre-filling the forms as following:

如何告知Laravel不要填写第二个表格?

推荐答案

没有Laravel的方法,但是您可以使用HTML基本表单数组来使其工作.您需要了解自己必须标识表单和字段,以便Laravel准确知道数据来自何处以及将数据发送回何处.如果您所有字段的名称都相同,怎么可能知道?

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?

这是一个概念证明,可以直接从您的routes.php文件使用.

This is a proof of concept that will work straight from your routes.php file.

正如我所做的那样,在发布我使用Route::get()Route::post()的答案之前在这里进行了测试,不必创建控制器和视图来测试我不会使用的东西.在开发此逻辑时,您将必须将此逻辑放在控制器中并从一个我认为它们已进入的角度来看.

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.

要对其进行测试,只需将浏览器指向以下路线即可:

To test it the way it is, you just have to point your browser to the following routes:

http://yourserver/form

当您按下按钮时,它将自动发布路线:

and when you push a button it will automatically POST tho the route:

http://yourserver/post

我基本上是给所有表单一个数字,并为按钮提供一个我们将在Laravel中使用的数字,以获取表单数据并对其进行验证.

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.

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();
});

这是在Blade视图中使用它的方式,现在使用3种形式,而不是2种形式,并且您可以根据需要拥有任意数量的形式:

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:

<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() }}

您甚至可以使用循环在刀片中创建100个表单:

And you can even use a loop to create 100 forms in blade:

@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会预先填写多种表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆