I have a Laravel 9.x project, and when I submit the form and an error occurs, I have managed to keep the already inserted value of the text fields, but I would like the checkboxes to keep their value as well.
<input type="checkbox" name="abono_tasa" value="{{ isset($vado->abono_tasa) ? ($vado->abono_tasa==1 ? 'checked': '') : '' }}" id="abono_tasa">
I would like to control this.
2
Answers
In Laravel, you can keep the old value of a checkbox input when a form is submitted and there are validation errors, using the
old
function in the Blade templates.When you’re rendering the form, you can use the
old
function to set the checkbox to its previous value if there are validation errors. Here’s an example:For convenience, you may use the @checked directive to easily indicate if a given HTML checkbox input is "checked". This directive will echo checked if the provided condition evaluates to true:
Thank you.
(Please, consider making this answer the selected answer if the solution works for you).
For variable-based default value
In below, if
$myDefault
is set totrue
, it equals checking the check-box by default.Always checked by default
Always un-checked by default
Below does not need "
!old()
" workaround.