Laravel FormRequest validation for dynamic rows

259 views Asked by At

I have a form with dynamics rows. I would like to use FormRequest to validate data rows. How to achieve it ?

Form

<td>
   <input class="form-control form-control-sm" id="item_quantity" type="number" step="any" min="0" name="item_quantity[]" value="{{old('item_quantity[]')}}"> 
   {!!$errors->first('item_quantity', '<span class="error">:message</span>')!!}
</td>

My FormRequest Code :

public function rules()
{
    return [
        'item_quantity' => 'required'
    ];
}
2

There are 2 answers

0
Black-Rabbit On

Finally i made these modifications and its works:

Form

<td><input class="form-control form-control-sm" id="item_quantity" type="number" step="any" min="0" name="item_quantity[]" value="{{old('item_quantity[]')}}">{!!$errors->first('item_quantity.*', '<span class="error">:message</span>')!!}</td>

FormRequest

public function rules(){return ['item_quantity.*' => 'required'];}

Thks for your helps !

3
It's VIN On

1: Do you save it as array or per row?

you can pass only required data as validated by form by adding required at end of input.

<td>
   <input class="form-control form-control-sm" id="item_quantity" type="number" step="any" min="0" name="item_quantity[]" value="{{old('item_quantity[]')}}" required=""> 
   {!!$errors->first('item_quantity', '<span class="error">:message</span>')!!}
</td>

if this does not work, please lets know how you save your data as per question 1 above.