Skip Navigation

[Resolved] I need Validation for a Checkbox field

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 5.52 hours from now. Thank you for your understanding.

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 3 years, 6 months ago.

Assigned support staff: Nigel.

Author
Posts
#907196

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

#907317

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.