lundi 26 février 2018

Set rules to an appended input Laravel 5.5

So i am working this project that has a feature of adding multiple rows in one submit, I am Using jQuery append() to add input rows, but then if one of the request did not comply to the required rules in validation the appended row is not showing and not returning its old input. here's my code.

My View

    <tr class="table_row" id="appended_tr">
      <td>
       <select id="menu" name="menu[]" class="form-control">   
         <option>Select</option>   
           @foreach($menus as $data)  
              <option value="" {!! old('menu.0') == $data->menu_id ? 'selected' : '' !!}></option>
           @endforeach
       </select>
       <small class="text-danger"></small>
  </td>
</tr>

here's My javascript

 $(document).ready(function() {
        $("#add_row").on("click", function() {
            $('select').select2('destroy');
            var count = $('tr[id="appended_tr"]').length;
            console.log();
            var content ='<tr id="appended_tr">' + 
                            '<td>' +
                                '<select id="menu" name="menu[]" class="form-control">' + 
                                    '<option>Select</option>' +   
                                    '@foreach($menus as $data)' +
                                        '<option value=""' +
                                        '{!! old("menu.1") == $data->menu_id ? "selected" : "wew" !!} ></option>' +
                                    '@endforeach' +
                                '</select>' +
                                '<small class="text-danger"></small>' +
                            '</td>' +
                        '</tr>';

        $("#table_body").append($(content));
        $('select').select2({width:400});
        });

     });

Here's my rules

 public function rules(){

    foreach($this->request->get('menu') as $key => $value){

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

    }

    return $rules;

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire