I am using Laravel 5.3
,this is a form demo below:
<div class="container">
<form>
<div class="form-group row">
<label for="name" class="col-sm-2 col-form-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" value="{{ old('name', $student->name)}}">
</div>
</div>
<fieldset class="form-group row">
<legend class="col-form-legend col-sm-2">Gender</legend>
<div class="col-sm-10">
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="radio" name="gender" value="1" @if(old('gender',$student->gender)=="1") checked @endif>
Male
</label>
</div>
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="radio" name="gender" value="2" @if(old('gender',$student->gender)=="2") checked @endif>
Female
</label>
</div>
</div>
</fieldset>
<div class="form-group row">
<label class="col-sm-2">Hobbies</label>
<div class="col-sm-10">
<div class="form-check">
<label class="form-check-inline">
<input class="form-check-input" type="checkbox" name="hobby[]" value="1" @if(...) checked @endif> football
</label>
<label class="form-check-inline">
<input class="form-check-input" type="checkbox" name="hobby[]" value="2" @if(...) checked @endif> basketball
</label>
<label class="form-check-inline">
<input class="form-check-input" type="checkbox" name="hobby[]" value="3" @if(...) checked @endif> swimming
</label>
</div>
</div>
</div>
<div class="form-group row">
<div class="offset-sm-2 col-sm-10">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
</div>
My question is:
I can show old data in input type="text"
like this:
<input type="text" class="form-control" id="name" name="name" value="{{ old('name', $student->name)}}">
And I can show old data in input type="radio"
like this:
<input class="form-check-input" type="radio" name="gender" value="1" @if(old('gender',$student->gender)=="1") checked @endif>
But I don't know how to show old data in input type="checkbox"
:
<input class="form-check-input" type="checkbox" name="hobby[]" value="3" @if(...) checked @endif>
How to do it?
Update for Laravel 9.x as Yusuf T suggested (refer this document) :
<input type="checkbox" name="active" value="active" @checked(old('active', $someVariableOrValue )) />
For older versions this will work:
<div class="form-check">
<label class="form-check-inline">
<input class="form-check-input" type="checkbox" name="hobby[]" value="1" @if(is_array(old('hobby')) && in_array(1, old('hobby'))) checked @endif> football
</label>
<label class="form-check-inline">
<input class="form-check-input" type="checkbox" name="hobby[]" value="2" @if(is_array(old('hobby')) && in_array(2, old('hobby'))) checked @endif> basketball
</label>
<label class="form-check-inline">
<input class="form-check-input" type="checkbox" name="hobby[]" value="3" @if(is_array(old('hobby')) && in_array(3, old('hobby'))) checked @endif> swimming
</label>
</div>
Also as brokekidweb suggested :
<label class="form-check-inline">
<input class="form-check-input" type="checkbox" name="hobby[]" value="1" {{ (is_array(old('hobby')) and in_array(1, old('hobby'))) ? ' checked' : '' }}> football
</label>
OR
<label class="form-check-inline">
<input class="form-check-input" type="checkbox" name="hobby[]" value="1" {{ (is_array(old('hobby')) && in_array(1, old('hobby'))) ? ' checked' : '' }}> football
</label>
You can use
<input class="form-check-input" type="checkbox" name="hobby[]" value="3" @if(in_array(3, old('hobby'))) checked @endif>
Or you could just switch to use Laravel Collective https://laravelcollective.com/docs/5.3/html#checkboxes-and-radio-buttons
And have code like this instead:
{!! Form::checkbox('name', 'value', true) !!}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With