Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel request validation with multiple forms on the same page

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 Form errors

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?

like image 787
bohdan baida Avatar asked Oct 17 '22 15:10

bohdan baida


1 Answers

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.

like image 136
FONGOH MARTIN Avatar answered Oct 21 '22 00:10

FONGOH MARTIN