I have a form that contains gender input in the form of a radio button, but when I try to submit with an empty value, the radio button element doesn't get into the Request object which in the example below the input name is jenis_kelamin. But if jenis_kelamin has a value, then the jenis_kelamin element will be entered into the Request Object.
I need the element in the Request object because if the radio element is empty, there will be an error with the input message required. So, what I'm hoping for is how the radio input gets into the Request object when the value is empty and why this is happening.
Routes
Route::get('/mahasiswas/create', 'MahasiswaController@create')->name('mahasiswas.create');
Route::post('/mahasiswas', 'MahasiswaController@store')->name('mahasiswas.store');
Controller
public function create(){
return view('form-pendaftaran');
}
public function store(Request $request){
dump($request);
}
View
<form action="{{route('mahasiswas.store')}}" method="post">
@csrf
<div class="form-group">
<label>Jenis Kelamin</label>
<input type="radio" name="jenis_kelamin" id="laki-laki" value="L">
<label for="laki-laki">Laki-laki</label>
</div>
<button type="submit" class="btn btn-primary mb-2">Daftar</button>
</form>
Radio Input if the value is null
Radio Input if there a value
Notes: I'm using Laravel 7
You can solve it by putting a hidden input with the same name before the radio inputs
<form action="{{route('mahasiswas.store')}}" method="post">
@csrf
<div class="form-group">
<label>Jenis Kelamin</label>
<input type="hidden" name="jenis_kelamin" value="" />
<input type="radio" name="jenis_kelamin" id="laki-laki" value="L">
<label for="laki-laki">Laki-laki</label>
</div>
<button type="submit" class="btn btn-primary mb-2">Daftar</button>
</form>
If the user checks the radio input, then it will override the hidden input you set.
PS: The same fix applies for checkboxes too
PPS: Probably the fix written in the comments works too, but this one works regardless of the framework used
PPPS: This fix may become a problem if a query selection by the name attribute is done, but the developer must be aware of that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.