Validating a Laravel request with multiple forms on one page

I have three different forms on one page. All inputs have their own validation rules, the code in the request file has a structure similar to 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 try to submit one of the three forms, the other form returns with a message about empty fields. It is not related to each other.

Here is a screenshot of the page Form errors

Here are 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 are separate and must be presented separately.

How can I fix this problem?

+4
source share
2 answers

, , 3 , .

( ), , , , .

unit_in_stock - unit_price_gbp - return_item

, -

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
            ];
    }
}

, , , .

, .

+1

3 , 3 URL-, .

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

, , . , ?

0

Source: https://habr.com/ru/post/1673077/


All Articles