Laravel 5 多字段验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30270604/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:33:57  来源:igfitidea点击:

Laravel 5 Multiple Fields Validation

validationlaraveleloquentlaravel-5

提问by Einlanzer

I have a field in the view the go something like

我在视图中有一个字段,类似于

<div>
  <input type="text" name="new[0][description]" id="description-new-0">
  <input type="text" name="new[0][amount]" id="amount-new-0">
</div>

<div>
  <input type="text" name="new[1][description]" id="description-new-1">
  <input type="text" name="new[1][amount]" id="amount-new-1">
</div>

and so on... so you can imagine that its a dynamic field that adds in the form every time you tick the add button or whatever.

等等......所以你可以想象它是一个动态字段,每次你点击添加按钮或其他任何东西时都会在表单中添加。

The question is how can I VALIDATE these dynamic fields and will return the right error for each fields?

问题是如何验证这些动态字段并为每个字段返回正确的错误?

Thanks!

谢谢!

Note this is Laravel 5 but if you have a Laravel 4 solution similar to this one I guess(really guess) it will work.

请注意,这是 Laravel 5,但如果你有一个类似于这个的 Laravel 4 解决方案,我猜(真的猜)它会起作用。

Thanks guys!

谢谢你们!

回答by dyachenko

create in folder Requests validator for this form like

在文件夹中为此表单创建请求验证器,例如

<?php
use Illuminate\Foundation\Http\FormRequest;

class MultipleRequest extends FormRequest
{

    /**
     * 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()
    {
        $rules = [
            'description' => 'required|array',
        ];

        if ($this->request->get('description')) {
            foreach($this->request->get('description') as $key => $val)
            {
                $rules['description.'.$key] = 'required|min:7'; //example
            }
        }

        return $rules;
    }


    public function messages()
    {
        $messages = [];
        if ($this->request->get('description')) {
            foreach ($this->request->get('description') as $key => $val) {
                $messages['description.' . $key . '.min'] = 'Wrong field.';
                $messages['description.' . $key . '.required'] = 'This field required.';
            }
        }
        return $messages;
    }
}

more info How To: Validate an array of form fields with Laravel

更多信息如何:使用 Laravel 验证表单字段数组

then in view do the next

然后在视图中做下一个

@if (Session::has('_old_input'))
        @for ($i=0; $i<count(Session::get('_old_input.description')); $i++)
            <div>
                @if($errors->any() && Session::get('errors')->getBag('default')->has('description.' . $i))
                    <p class="">{{Session::get('errors')->getBag('default')->first('description.' . $i)}}</p>
                @endif


                <input type="text" name="new[][description]" id="description-new-{{$i}}" value="{{Session::get('_old_input.description.' . $i)}}">
                <input type="text" name="new[][amount]" id="amount-new-{{$i}}" value="{{Session::get('_old_input.amount.' . $i)}}">
            </div>
        @endfor
    @endif

so you add block with error message for each block with inputs. In my example only description processed, amount you can processed similar description For my it works and look like enter image description here

所以你为每个带有输入的块添加带有错误消息的块。在我的例子中只处理了描述,你可以处理类似描述的数量对于我来说它是有效的,看起来像 在此处输入图片说明

UPD:Laravel version 5.2 has array validation so you can create request validator like:

UPD:Laravel 5.2 版具有数组验证功能,因此您可以创建请求验证器,例如:

public function rules()
{
    return [
        'names.*' => 'required|max:50',
        'emails.*' => 'required|max:100',
    ];
}

for more info read DOC

了解更多信息,请阅读DOC