I have a form with "Does this case have IR number?"
If yes, show fields. If no, hide and show others.
I am using
validate()function.old()to keep data after failed form submission, akavalidate()error messages.- JQuery show/hide for the fields.
For the radio input, I used old() like this:
<input class="btn-check" type="radio" name="checkIR" value="haveIR" @if(!old('checkIR')) checked @endif id="haveIR" required>
<input class="btn-check" type="radio" name="checkIR" value="noIR" @if(old('checkIR')) checked @endif id="noIR">
to keep the checked as it is after failed validate(), but it's buggy, When I check "Yes" and refresh, it checks "No" when it must be as "Yes".
As for the show/hide, here is what I did:
// Show/Hide fields based on radio button option
$(document).ready(function() {
$('input[name="checkIR"]').click(function() {
var inputValue = $(this).attr("value")
var targetField = $("." + inputValue);
$(".box").not(targetField).slideUp();
$(targetField).slideDown();
});
});
With a help of css:
.box {
display: none;
}
How the code works:
If yes/haveIR radio is checked: show all fields containing class="box haveIR"
Issues trying to solve:
- How to fix/improve the small bug in the input
old()? - If user checked "yes", how to keep the fields of
yesvisibile even after failed laravelvalidate()?
ddyour$request->all()to see what are the actual values forcheckIr?"checkIR" => "haveIR"when "Yes" is checked. AndnoIRif "No" is checked