i dont know why my radio button checked by default the last element in laravel collective,in my case the radio button is using for select gender,each element contain representative of the gender as alphabet 'L' or 'P',thank in advance,
<div class="form-group">
{{-- <label for="jenisKelamin" class="control-label">Jenis Kelamin</label> --}}
{{Form::label('jenisKelamin','Jenis Kelamin',['class'=>'control-panel'])}}
<div class="form-check">
{{-- <label>
<input type="radio" name="jenisKelamin" id="jenisKelamin" value="L" class="form-control" placeholder="Jenis Kelamin" >Laki-Laki</label> --}}
{{Form::radio('jenis_kelamin','L',['class'=>'form-check-input'])}}
<label class="form-check-label">Laki-Laki</label>
</div>
<div class="form-check">
{{Form::radio('jenis_kelamin','P',['class'=>'form-check-input'])}}
<label class="form-check-label">Perempuan</label>
</div>
</div>
@if($errors->has('jenis_kelamin'))
<br>
<div class="alert alert-danger">{{ $errors->first('jenis_kelamin') }}</div>
@endif
</div><br>
I assume you are using the laravelcollective/html
package. When I view the raw HTML output in my internet browser, this is what I see:
<div class="form-group">
<label for="jenisKelamin" class="control-panel">Jenis Kelamin</label>
<div class="form-check">
<input checked="checked" name="jenis_kelamin" type="radio" value="L">
<label class="form-check-label">Laki-Laki</label>
</div>
<div class="form-check">
<input checked="checked" name="jenis_kelamin" type="radio" value="P">
<label class="form-check-label">Perempuan</label>
</div>
</div>
</div><br>
Both radio input's are checked (have checked="checked"
). Since both have the same name, meaning only one can be checked at a time, only the last is checked on screen.
Let's look at the source for the Form::radio
function. It can be found in \vendor\laravelcollective\html\src\FormBuilder.php
in your project, or in the Github repository. Here we see the function has this header:
public function radio($name, $value = null, $checked = null, $options = [])
It seems the third argument should be a boolean value determing the checked status, and any additional options as the fourth argument. This is also shown in the documentation.
In your snippet it would look like this:
{{Form::radio('jenis_kelamin','L', true, ['class'=>'form-check-input'])}}
{{Form::radio('jenis_kelamin','P', false, ['class'=>'form-check-input'])}}