user2566626 user2566626 - 9 months ago 178
PHP Question

Form array validation Laravel 5.2

I am currently stuck on solving this problem. I am new to Laravel and the MVC framework. I am struggling to create the dynamic form that gives the user the ability to add as many forms as possible. When the user enters the page at first it generates 5 form fields . Here is a look at my code so far.

<div id ={{$id = "from".$i}} >

<div class="form-group col-md-6">

<div class="col-md-6 form-group">

<label for={{$id = "Address".$i}}>Address</label>
<input type="text" name = "address[{{$i}}]" class="form-control" id={{$id = "Address".$i}} placeholder="Street Address"> <!-- problem form array how does this work in laravel -->


<div class="form-group col-md-6">

<label for={{$id = "city".$i}}>City</label>
<input type="text" value = "{{ old('city') }}" class="form-control" id={{$id = "City".$i}} placeholder="City">

@if ($errors->has('city'))
<span class="help-block">
<strong>{{ $errors->first('city') }}</strong>

How would I go about validating a form in Laravel 5.2 with from array

here's my controller

public function Postdata(Request $request) {

$this->validate($request->all(), [
'address.*' => 'required|string',
'city' => 'required',


I am using a for loop to generate the forms dynamically.

here is the error I get

ErrorException in ValidatesRequests.php line 49:
Argument 1 passed to App\Http\Controllers\Controller::validate() must be an instance of Illuminate\Http\Request, array given, called in
C:\wamp\www\Dynamic- 1.0\app\Http\Controllers\propContoller.php on line 34 and defined

Can someone please help or point me in the right direction thank you !

  1. add name="city[{{$i}}]"
  2. Create a specific request with php artisan make:request PostRequest
  3. Change public function Postdata(Request $request) { to public function Postdata(PostRequest $request) {
  4. Remove the validate function call from your controller
  5. Go to /app/Http/Requests/PostRequest.php
  6. Then edit the rules function to something like this ...


 * Get the validation rules that apply to the request.
 * @return array
public function rules()
    $rules = [];

    foreach($this->request->get() as $key => $val)
        $rules['address.' . $key] = 'required';
        $rules['city.' . $key] = 'required';

    return $rules;