Gravity Forms – How to require all checkboxes ticked

The following code shows how to make a checkbox field in Gravity Forms require all options be ticked if the field is required.

By default, if the field is required only one needs to be ticked.

If youโ€™re not sure where to place this code I highly recommend you read How to create a WordPress plugin for your custom functions.

Note: this uses the gform_field_validation filter that allows you to filter by form or form and field. See the Gravity Forms documentation for examples of this.

add_filter( 'gform_field_validation', 'checkbox_all_required', 10, 4 );
function checkbox_all_required( $result, $value, $form, $field ) {
	if ( 'checkbox' == $field->get_input_type() && $field->isRequired ) {
		foreach ( $value as $choice ) {
			if ( empty( $choice ) ) {
				$result['is_valid'] = false;
				$result['message'] = 'All checkboxes must be ticked.';
				break;
			}
		}
	}
    return $result;
}

Tagged in

5 comments on “Gravity Forms – How to require all checkboxes ticked

  1. This will work on multi-step gravity forms. It also checks certain form and field IDs.

    add_filter( ‘gform_field_validation’, ‘checkbox_all_required’, 10, 4 );
    function checkbox_all_required( $result, $value, $form, $field ) {
    if ( 6 == $form[‘id’] && 31 == $field->id ) { // Check if Form ID is 6 and Field ID is 31
    if ( ‘checkbox’ == $field->get_input_type() && $field->isRequired ) {
    foreach ( $value as $choice ) {
    if ( empty( $choice ) ) {
    $result[‘is_valid’] = false;
    $result[‘message’] = ‘All checkboxes must be ticked.’;
    break;
    }
    }
    }
    }
    return $result;
    }

  2. To have a specific form and field targeted, instead of add_filter( ‘gform_field_validation’, ‘checkbox_all_required’, 10, 4 );
    add the form ID and in option the field id like this add_filter( ‘gform_field_validation_FORMID_FIELDID’, ‘checkbox_all_required’, 10, 4 );

  3. perfect, i searched for this.

    But will this automatically work for all the forms i already made? I am asking since i have several dozens on my site and i don’t want to have to setup each one of them again….

    Thanks!
    Michael

Leave a Comment

Your email address will not be published. Required fields are marked *