I need to do exactly what this guy did: https://toolset.com/forums/topic/cant-validate-checkboxes/ except that I am not using CRED form.
Is it possible to force validation of a checkbox field in my Post Fields area or via a code snippet like the one in the linked thread?
Thanks,
Marcus
Nigel
Supporter
Languages:
English (English )
Spanish (Español )
Timezone:
Europe/London (GMT+00:00)
Hi Marcus
You need this for a checkboxes field (plural), right? Where at least one of the checkboxes must be checked.
That's not too difficult to do with CRED forms, as described in the other thread, as you can use the validation hook of the CRED API.
There is no equivalent hook to do the same in core WordPress (but it might be added: https://core.trac.wordpress.org/ticket/43019 ).
The only alternative I can think of is to write a bespoke JavaScript solution to disable submitting the form until one of the checkboxes has been checked.
For information about the best way to enqueue JS on the admin post edit screen for custom post types, see this: https://wordpress.stackexchange.com/a/243738/35739
The JS you write itself will want setting up something like this:
( function( $ ) {
$( document ).ready( function(){
// Your code here
});
})( jQuery );
You can use the jQuery change function to trigger some code when the checkboxes fields are checked (hidden link ).
Writing such code is outside our support policy, I'm afraid, and the queue is too busy for me to go much further than the above advice.
Sorry I can't be more helpful.