0
votes

i use Laravel and i have a form with laravel validation and required fields (so when I get errors, it reload always all page, and I keep values with old blade tag). All good, but I did a button to add some text boxes with jquery

$(".add-more").click(function(){ 
      var html = $(".copy").html();
      $(".after-add-more").before(html);
    });

    $("body").on("click",".remove",function(){ 
      $(this).parents(".control-group").remove();
    });

How can i save old values and the created n-html elements when Laravel reload all page at every invalid message after submission ? Thx

2
Your question is not clear try adding some code so that we can understand. - Akhtar Munir
Best practice would be to not reload the page and keep the html as is. Manage the validations without reload. - gaganshera
Also in Laravel framework? I know that Laravel always reload, for this exists old value retriever.....are u sure? - Marco Bozzola
Well I would suggest to use localStorage save data into localstorage, retrieve when you need it, but remember to empty localstorage on each reload page. - Akhtar Munir

2 Answers

0
votes

Its quite simple, you dont reload the whole page, include an element with laravel's @include for example, the contents of the table are in another blade file, but it is included

<table class="table tablesorter ">
    <thead class=" text-primary">
        <tr>
            <th scope="col">ID</th>
            <th scope="col">TIPO</th>
            <th scope="col">fecha</th>
            <th scope="col">Monto</th>
            <th scope="col">Acciones</th>
        </tr>
    </thead>
    <tbody id="table">
        @include('component-table')
    </tbody>
</table>

component-table.blade.php


    <tr>
        <td>hi</td>
        <td>
            test
        </td>
        <td></td>
        <td></td>
        <td class="text-right">

        </td>
    </tr>

Now, you can make a controller that returns this view and reload only the table when you call that controller and just replace the html with jquery's html() function

//on controller 
    public function table()
    {
        return view('component-table.blade.php');
    }
//js
    async function reloadTable(){
        await $.ajax({
            type: "GET",
            url: "{{ route('component-table') }}",
            data: '',
            success: function(response){
                            $('#table').html(response)
                        }
        });
    }
0
votes

At least working with blade i obtained this, i dunno if is correct. Resume what i have. A form with several fields and one button that add other 2 html field each times u press this(one is a combobox, with name='tipo' that take values from a table (called moyens) in the db)

@if( old('tipo') )
   @foreach (old('tipo') as $key => $value) 
    <!--<div>{{ $key . ' => ' . $value }}</div>-->
    <div class="col-sm-5">
    <select class="form-control" name="tipo[]">
        @foreach($moyens as $moyen)
            <option value="{{$moyen->code}}" @if($moyen->code == '$value') 'selected' @endif>{{ $moyen->name }}</option>
        @endforeach
    </select>
    </div>
    <div class="col-sm-4">
        <div class="input-group-prepend">
            <span class="input-group-text">&#8364</span>
            <input type="number" min="0" step="0.01" data-number-to-fixed="2" data-number-stepfactor="100" class="form-control" name="importo[]" placeholder="importo" value="{{old('polizza')}}">
        </div>
        </div>
        <div class="col-sm-3">
        <button class="btn btn-danger remove" type="button"><i class="glyphicon glyphicon-remove"></i> Cancella</button>
    </div>
    @endforeach
@endif

All works, but I CANNOT retrieve the old selected values in the combobox/es

Is this part

 <option value="{{$moyen->code}}" @if($moyen->code == '$value') 'selected' @endif>{{ $moyen->name }}</option>

Can be correct this approch? Thx