Title: Debugging my wpcf7_validate_checkbox function
Last modified: August 30, 2016

---

# Debugging my wpcf7_validate_checkbox function

 *  Resolved [nolaflash](https://wordpress.org/support/users/nolaflashcom/)
 * (@nolaflashcom)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/debugging-my-wpcf7_validate_checkbox-function/)
 * Am attempting to add conditional requirement of agree-to-terms based on a ‘Yes’
   value from field New.
 * Issue is that when agree-to-terms is required it appears in $result with default
   text regardless of field new’s value like so
 *     ```
       [agree-to-conditions] => Array
           (
              [reason] => Please fill the required field.
              [idref] =>
       )
       ```
   
 * When agree-to-terms is not required it is never in $result and the custom error
   message never appears and form is read as valid regardless of checkbox state.
 * Form fields are currently:
 *     ```
       [select new "No" "Yes"]
       [checkbox* agree-to-conditions "I agree to the Terms and Conditions"]
       ```
   
 * functions.php code is executing (tested with mail() calls). The values of new
   and agree-to-terms are seen in callback as expected.
 *     ```
       /* CUSTOM VALIDATION FOR CF7 FORM Place a CARE Account  */
       add_filter( 'wpcf7_validate_checkbox', 'care_account_valid', 10, 2 );
       add_filter( 'wpcf7_validate_checkbox*', 'care_account_valid', 10, 2 );
   
       function care_account_valid( $result, $tag ) {
       	$conditions = $_POST['agree-to-conditions'];
       	if ($tag['name'] == 'new' && $_POST['new'] == 'Yes') {
       		if ($conditions[0] != 'I agree to the Terms and Conditions') {
       			$result['valid'] = false;
       			$result['reason']['agree-to-conditions'] = "Please check the box to accept the Terms and Conditions";
       		}
       		mail('designs@nolaflash.com', 'test 1.1', $conditions[0] . $_POST['new'] . print_r($result,1));
       	} else if ($tag['name'] == 'new' && trim($_POST['new']) != 'Yes') {
       		$result['valid'] = true;
       	}
   
       	return $result;
       }
       ```
   
 * Any advice for getting the validation and error message for agree-to-terms dependent
   on value of new?
 * [https://wordpress.org/plugins/contact-form-7/](https://wordpress.org/plugins/contact-form-7/)

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Plugin Author [Takayuki Miyoshi](https://wordpress.org/support/users/takayukister/)
 * (@takayukister)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/debugging-my-wpcf7_validate_checkbox-function/#post-6558652)
 * How about simply using `checkbox` instead of `checkbox*` if it’s not supposed
   to be a required field?
 * Also, as a whole your coding looks outdated. I suggest giving this a read.
 * [http://contactform7.com/2015/03/28/custom-validation/](http://contactform7.com/2015/03/28/custom-validation/)
 *  Thread Starter [nolaflash](https://wordpress.org/support/users/nolaflashcom/)
 * (@nolaflashcom)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/debugging-my-wpcf7_validate_checkbox-function/#post-6558717)
 * Thank you for the reply!
 * I meant to mention that I had tried checkbox without requiring it via * but found
   that when not required if I mailed a var_dump() of $result that agree-to-terms
   never showed up in the array at all when not required.
 * Anyway I will definitely read the link you sent right now.
 * The time you take helping us use your free and fantastic plugin is GREATLY appreciated!
 *  Thread Starter [nolaflash](https://wordpress.org/support/users/nolaflashcom/)
 * (@nolaflashcom)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/debugging-my-wpcf7_validate_checkbox-function/#post-6558720)
 * This works!! Thank you for the link, the plugin, and the nice new OOP code. My
   Friday is better already! Small present sent via PayPal.
 *     ```
       add_filter( 'wpcf7_validate_checkbox', 'care_account_valid', 20, 2 );
   
       function care_account_valid( $result, $tag ) {
           $tag = new WPCF7_Shortcode( $tag );
           if ( 'agree-to-conditions' == $tag->name ) {
               $new = isset( $_POST['new'] ) ? trim( $_POST['new'] ) : '';
               if ( $new == 'Yes' && !isset($_POST['agree-to-terms'])) {
                   $result->invalidate( $tag, "Please check the box to accept the Terms and Conditions." );
               }
           }
           return $result;
       }
       ```
   
 *  Thread Starter [nolaflash](https://wordpress.org/support/users/nolaflashcom/)
 * (@nolaflashcom)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/debugging-my-wpcf7_validate_checkbox-function/#post-6558730)
 * Seems like a link to [http://contactform7.com/2015/03/28/custom-validation/](http://contactform7.com/2015/03/28/custom-validation/)
   from the Tips section on [http://contactform7.com/docs/](http://contactform7.com/docs/)
   or from Additional Settings page would be a great idea!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Debugging my wpcf7_validate_checkbox function’ is closed to new replies.

 * ![](https://ps.w.org/contact-form-7/assets/icon.svg?rev=2339255)
 * [Contact Form 7](https://wordpress.org/plugins/contact-form-7/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/contact-form-7/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/contact-form-7/)
 * [Active Topics](https://wordpress.org/support/plugin/contact-form-7/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/contact-form-7/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/contact-form-7/reviews/)

## Tags

 * [validation](https://wordpress.org/support/topic-tag/validation/)

 * 4 replies
 * 2 participants
 * Last reply from: [nolaflash](https://wordpress.org/support/users/nolaflashcom/)
 * Last activity: [10 years, 8 months ago](https://wordpress.org/support/topic/debugging-my-wpcf7_validate_checkbox-function/#post-6558730)
 * Status: resolved