Laravel请求在同一页面上使用多种表单进行验证 [英] Laravel request validation with multiple forms on the same page

查看:210
本文介绍了Laravel请求在同一页面上使用多种表单进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一页面上有三种不同的形式.所有输入都有自己的验证规则,请求文件中的代码具有以下结构:

I have three different forms on the same page. All inputs has it's own validation rules, code in Request file has structure like this:

public function rules()
{
    return [
        //
        'sold'  => 'required',
        'unit_in_stock'  => 'required',
        'unit_price_gbp' => 'required',
        'returned_item'  => 'required',
    ];
}


public function messages()
{
    return [
        'sold.required' => 'Please enter quantity of sold parts',
        'unit_in_stock.required' => 'Please enter quantity of sold parts',
        'unit_price_gbp.required' => 'Please enter price in GBP',
        'returned_item.required' => 'Please enter quantity of items',
    ];
}

但是,当我尝试提交三种形式之一时,另一种形式会返回有关空字段的消息.这种形式互不相关.

But when I am trying to submit one of three forms, another form returns with message about empty fields. This forms not related with each other.

这是页面的快照

这是我的表格:

{!! Form::open(['url' => route('addDelivery.addDelivery'),'class'=>'contact-form','method'=>'POST']) !!}

    <label>Price in GBP &#163;:</label>
    {!! Form::text('unit_price_gbp', isset($price->unit_price_gbp) ? $price->unit_price_gbp : old('unit_price_gbp'), array('class'=>'form-control'), ['placeholder'=>'GBP']) !!}
    <label>Quantity: </label>
    {!! Form::text('unit_in_stock', isset($price->unit_in_stock) ? $price->unit_in_stock : old('unit_in_stock'), array('class'=>'form-control'), ['placeholder'=>'Qt.']) !!}

    <input type="hidden" name="part_number" value="{{ $product->part_number }}">
    <input type="hidden" name="part_id" value="{{ $product->id }}">
    <input type="hidden" name="slug" value="{{ $product->slug }}">

    {!! Form::button('Add Delivery', ['class' => 'btn btn-sm btn-success','type'=>'submit']) !!}

{!! Form::close() !!}


{!! Form::open(['url' => route('sold.sold'),'class'=>'contact-form','method'=>'POST']) !!}

    <label style="margin-right: 30px;">Sell:</label>
    {!! Form::text('sold', old('sold'), array('class'=>'form-control', 'placeholder'=>'Qty.')) !!}
    <input type="hidden" name="part_id" value="{{ $product->id }}">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    {!! Form::button('Sold', ['class' => 'btn btn-sm btn-success','type'=>'submit']) !!}

{!! Form::close() !!}


{!! Form::open(['url' => route('productReturn.productReturn'),'class'=>'contact-form','method'=>'POST']) !!}

    <label>Return:</label>
    {!! Form::text('returned_item', old('returned_item'), array('class'=>'form-control', 'placeholder'=>'Qty.')) !!}
    <input type="hidden" name="part_id" value="{{ $product->id }}">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    {!! Form::button('Return', ['class' => 'btn btn-sm btn-success','type'=>'submit']) !!}

{!! Form::close() !!}

但是所有三种表格都是分开的,需要分别提交.

But all three forms is separate, and need to be submitted separately.

我该如何解决此问题?

How can i fix this problem?

推荐答案

从我的角度来看,您的规则跨度约3种不同的形式,这就是为什么在请求中,它会检查所有这些规则的一种形式.

From what i see, your rules are span accros 3 different forms thats why on the request, its checking all those rules on one form.

放置单独的规则数组(但是您可以使用相同的消息数组)以应用于特定的表单验证,那么您将不会获得另一表单的规则应用于您提交的表单.

Put separate rules array (but you can use same message array) to apply to the specific form validation then you will not get the rules of another form applied to the form you submitting.

出售的是第二种形式的字段 unit_in_stock是第一种形式的字段 unit_price_gbp是第一种形式的字段 return_item是第三种形式的规则

sold is field in second form unit_in_stock is field in first form unit_price_gbp is field in first form returned_item is rule in third form

所以我的建议是将规则函数重写为类似

So my advice will be to rewrite the rules function to something like this

public function rules($formType)
{
    switch($formType){
        case "addDelivery":
            return [
                //put only add delivery validation rules here
            ];
        break; 
        case "sold":
            return [
                //put only sold validation rules here
            ];
        break; 
        case "productReturn":
            return [
                //put only product return validation rules here
            ];
    }
}

那时,只要您需要规则并调用规则函数并传递正确的表单名称,它就会为您返回所需的正确验证规则集.

At that point, anytime you need the rules and call the rules function and pass in the right form name, then it will return you the right set of validation rules you need.

希望有帮助.

这篇关于Laravel请求在同一页面上使用多种表单进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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