Laravel Form helper not show data after validation error

408 views Asked by At

i'm having problem with forms helper in laravel 5.3. I have a foreach that shows my data in a form. Everythins work fine, until i submit a form and validation fails. Therefore, when validation fails, the input text in my form not show the previus data, but only the data which were submitted. Here the code:

@foreach($cars as $car)
    {!! Form::model($car, ['route' => ['cars.update', $car->id], 'method' => 'PUT']) !!}
        <div class="col-md-2">
            {{ Form::label('name', $car->name) }}
            {{ Form::text('name', $car->name, ['class' => 'editbox-normal']) }}
        </div>
        <div class="col-md-2">
            {{ Form::label('phone', 'Phone') }}
            {{ Form::text('phone', null, ['class' => 'editbox-normal']) }}
        </div>
        <div class="col-md-2">
            {{ Form::label('license_plate', 'License plate') }}
            {{ Form::text('license_plate', null, ['class' => 'editbox-normal']) }}
        </div>
        <div class="col-md-2">
            {{ Form::label('total_km', 'KM') }}
            {{ Form::text('total_km', null, ['class' => 'editbox-normal']) }}
        </div>
        <div class="col-md-1 col-md-offset-3 flex-space-around">
            {{ Form::submit('Submit', ['class' => 'btn btn-edit']) }}
        </div>
    {!! Form::close() !!}
@endforeach

If i submit one of those forms and validation fails, for example name is required and name input is empty, laravel flash $error, but every input of each form displays the data of the submitted form. In this example, every name input will be empty, and phone, license_plate and total_km will have the same value. Instead, if i use pure html code for the input, everythinks work fine. In addition, if you notice, the fist label has the value set to $car->name, the same of the text input, but in the label it is shows correctly, in the input not. Anyone knows why? Thanks in advance.

1

There are 1 answers

2
Daan Meijer On

This is actually intended behaviour. This way, when you e.g. forget to enter your name, the rest of the data you entered isn't lost. In your use case, however, this leads to unintended results (because you have multiple forms on the same page). I would advise splitting the forms up to different pages :)