You have an array element that represents a list of all the checkbox id’s that are checked. If you page has checkboxes checked on load, then make sure those id’s are populated in the array at load. The value for each checkbox must be unique for this to work.
The last remaining checkbox that is checked, will be disabled, and thus prevents it from being unchecked. The other checkboxes may still be checked. It you don’t want it to look disabled, you can just add css for the :disabled selector for these checkboxes.
There is a problem with your solution. It indeed works that you disable the last checked checkbox, but when you submit the form the disabled inputs will not be send. If you do not submit the form then you could use it.
Hey @brad,
Just pinched this from one of our old Projects, radio group though but similar principle should apply to a checkbox group? You can see the name for all the radios is the same but the id’s are different. As long as the names are the same and you apply validation to one of the radios the validation should work for all…
Alternatively what about doing the validation on the server side rather than Client side? That way there is only the one input for the group to validate?
Alternatively what about doing the validation on the server side rather than Client side? That way there is only the one input for the group to validate?
Is there a way to show a validation message with server side validation?
So I ended up having to do a hybrid of both solutions. I had to have a hidden field with the selected values and use server side validation for validation (as @Cheese suggested) and submit the hidden field value to the data insert.
@franse method worked except for the case that if you submitted without selecting anything the validation would work, but then if you selected more than one option in the checkboxes it would only take the first checked value not all of them.
By combining the two solutions and getting a little creative I was able to achieve the desired results except for the CSS styling. The CSS that @franse provided only works for client side validation.
The form you posted works fantastic if submitted before an invalid submission. After an Invalid submission, for some reason it only accepts the value from the first one checked and the rest is ignored. No idea why.
As for the CSS, it turns red on invalid and remains red even after a valid choice was selected. For this use case, I can live with that. At least the selected choices turn green.
Thank you so much for your help. I have it working to a satisfactory level.
Would be great to have a built in Wappler way of required validation for Radio Buttons and CheckBoxes.