kriszpchicken's avatar

Show old values in edit form for checkbox and select

I made an edit function for my application, and when I click on it I can get the old values for the text inputs, but not for the select and checkbox ones. I am not sure how I should do it. Could you give me some suggestions please?

@extends('layout')

@section('title')
<title>Buch bearbeiten</title>
@section('stylesheets')
<script src="http://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />


@endsection
@section('content')
<style>
  .uper {
    margin-top: 40px;
  }
</style>

<div class="card uper">
    <div class="card-header">
        Buch bearbeiten
    </div>
    <div class="card-body">
        <form method="post" action="{{ route('books.update', $book->id) }}">
            <div class="form-group">
                @csrf
                @method('PATCH')
                <label for="title">Titel:</label>
                <input type="text" class="form-control" name="title" value="{{ $book->title }}" />
                @error('title')
                <div class="alert alert-danger">{{ $message }}</div>
                @enderror
            </div>
            <div class="form-group">
                <label for="author_id">Author(en):</label>
                <select name="author_id[]" multiple class="form-control select2-multi <!-- @error('author_id') is-invalid @enderror -->">
                    @foreach ($authors as $author)
                    <option value="{{ $author->id }}">{{ $author->name }}</option>
                    @endforeach
                </select>
                @error('authors')
                <div class="alert alert-danger">{{ $message }}</div>
                @enderror
            </div>
            <div class="form-group">
                <label for="year">Jahr:</label>
                <input type="text" class="form-control" name="year" value="{{ $book->year }}" />
                @error('year')
                <div class="alert alert-danger">{{ $message }}</div>
                @enderror
            </div>
            <div class="form-group">
                <label for="publishers">Verlag(e):</label>
                <select name="publishers[]" multiple class="form-control select2-multi <!-- @error('publishers') is-invalid @enderror -->">
                    @foreach ($publishers as $publisher)
                    <option value="{{ $publisher->id }}">{{ $publisher->name }}</option>
                    @endforeach
                </select>
                @error('publishers')
                <div class="alert alert-danger">{{ $message }}</div>
                @enderror
            </div>

            <div class="form-group">
                <label for="genres">Genre(s):</label>
                @foreach($genres as $genre)
                <input type="checkbox" name="genres[]" value="{{ $genre->id }}">{{ $genre->name }}
                @endforeach
                </select>
                @error('genre_id')
                <div class="alert alert-danger">{{ $message }}</div>
                @enderror
            </div>

            <div class="form-group">
                <label for="language_id">Sprache:</label>
                <select name="language_id" class="form-control @error('language_id') is-invalid @enderror">
                    <option value="{{ old('language_id')}}">-- {{ __('Spache auswählen') }} --</option>
                    @foreach ($languages as $language)
                    <option value="{{ $language->id }}">{{ $language->name }}</option>
                    @endforeach
                </select>
                @error('language_id')
                <div class="alert alert-danger">{{ $message }}</div>
                @enderror
            </div>
            
            <div class="form-group">
                <label for="isbn">ISBN:</label>
                <input type="text" class="form-control" name="isbn" value="{{ $book->isbn }}" />
                @error('isbn')
                <div class="alert alert-danger">{{ $message }}</div>
                @enderror
            </div>
            <div class="form-group">
                <label for="pages">Seitenzahl:</label>
                <input type="text" class="form-control" name="pages" value="{{ $book->pages }}" />
                @error('pages')
                <div class="alert alert-danger">{{ $message }}</div>
                @enderror
            </div>
            <button type="submit" class="btn btn-primary">Hinzufügen</button>
        </form>
    </div>
</div>
<script type="text/javascript">
        $(".select2-multi").select2();
	</script>
@endsection

0 likes
5 replies
GGio's avatar

You need to set checkbox checked & drop-down option as selected.

Something like:

<option value="{{ $author->id }}" {{ $book->author->id === $author->id ? 'selected' : '' }}>{{ $author->name }}</option>

and for checkbox:

<input type="checkbox" name="genres[]" value="{{ $genre->id }}" {{ $book->genre->id === $genre->id ? 'checked' : '' }}>

kriszpchicken's avatar

I don't think that works in my case, because I have several options for the checkbox, not just one.

kriszpchicken's avatar

I get the error message "Trying to get property 'id' of non-object" if I use that code in both cases.

jlrdw's avatar

You have to Loop through what's available versus what was stored, and of course if one available matches one that is currently stored well I think you get the idea.

Just example

Available if you have choices one, two, three, four, five

two and five are stored.

Loop over choices 1 through 5

With if statement if stored show in edit as selected.

You're just doing a simple comparison logic.

Please or to participate in this conversation.