1

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>


                  <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>
                                </span>
                            @endif
              </div>

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 !

2
  • Try using $request instead of $request->all() Commented May 3, 2016 at 19:01
  • Also it is better to define validation rules in your custom request Commented May 3, 2016 at 19:16

2 Answers 2

1
  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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Just a general idea. See if that helps.
0

Thank You man ! this is the solution I end up with

  <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">


                </div>

in post request i did this

public function authorize()
{
    return true;
}

      /**
        * Get the validation rules that apply to the request.
        *
        * @return array
       */

    public function rules() {

    $rules = [];

       foreach($this->request->get('address') as $key => $val) {

       $rules['address.'.$key] = 'required';

       }

       return $rules;
      }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.