Laravel request validation with multiple forms on the same page

半腔热情 提交于 2020-01-24 07:49:09

问题


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.

Here is a sreenshot of the page

Here is my forms:

{!! 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?


回答1:


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.

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.

Hope that helps.




回答2:


Make the 3 forms submitting to 3 different URLs but using the same function.

Route::post('form-1', 'Controller@store');
Route::post('form-2', 'Controller@store');
Route::post('form-3', 'Controller@store');

Perhaps simply make sure you are OPENING and CLOSING each form before starting the next one. Most likely your submit button submitted more than one form?



来源:https://stackoverflow.com/questions/42991026/laravel-request-validation-with-multiple-forms-on-the-same-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!