jeudi 22 septembre 2016

Array validation for table inputs in laravel 5.1

I am trying to give a validation for array inputs using table ,if the inputs are empty it should suppose to give a validation for each input box border as red color. My table code is like we can add multiple no of rows by click add button like My table view

My validations are coming like as in the below image shown My current validation output

and i want validation something like as below using $errors and has-feedback in bootstrap laravel My desired output

table view code:

<form action="" method="post"  enctype="multipart/form-data">
              <input type="hidden" name="_token" value="}" />
              <table class="table table-bordered table-striped-col nomargin" id="table-data">
                <tr align="center">
                  <td>Event Name</td>
                  <td>Event Code</td>
                  <td>Event Date</td>
                  <td>City</td>
                  <td>Country</td>
                </tr>
                <tr>

                    @if(null != old('eventname')) 
       @for($i=0;$i<count(old('eventname'));$i++)

          <td>
             <div class="form-group has-feedback " id="">
                <input type="text" class="form-control " autocomplete="off" name="eventname[]" value="">
                @if ($errors->has('eventname'.$i)) 
                    <p class="help-block"><span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
                    </p>
                @endif
            </div>
         </td>
      @endfor
  @else 
      <td>
        <div class="form-group has-feedback " id="">
            <input type="text" class="form-control " autocomplete="off" name="eventname[]" >
        @if ($errors->has('eventname')) 
           <p class="help-block"><span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
      </p>
        @endif
       </div>
      </td>
 @endif

                  <td>
                    <input type="text" class="form-control" autocomplete="off" name="eventcode[]" >
                  </td>
                  <td>
                    <input type="text" class="form-control dob"  autocomplete="off" name="date[]" >
                  </td>
                  <td>
                    <input type="text" class="form-control" autocomplete="off" name="city[]" >
                  </td>
                  <td>
                    <input type="text" class="form-control" autocomplete="off" name="country[]" >
                  </td>
                  <td>
                    <input type="button" value="+" class="add btn btn-success">
                    <input type="button" value="-" class="delete btn btn-danger">
                  </td>
                </tr>
              </table>
              </br>
              <center> <button type="submit" class="btn btn-primary" name="submit">Submit</button></center>
            </form>

my controller is like below:

public function postEvent( EventRequest $request ) {

            $data = Input::get();

            for($i = 0; $i < count($data['eventname']); $i++) {
                $c= new Event();

                $c->event = $data['eventname'][$i];
                $c->eventcode = $data['eventcode'][$i];
                $c->date = $data['date'][$i];
                $c->city = $data['city'][$i];
                $c->country  = $data['country'][$i];
                $c->save();

             }

              $request->session()->flash('alert-success', 'Event was successful added!');
              return redirect('executor/all');


}

and then my validation code in EventRequest.php

public function rules()
{



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


    return $rules;


}

I am using has-feedback error code inside my table for the first input .But still the validations are coming in the same way i could not able to find what was the mistake in my code.

Any help would be appreciated. Thank you



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire