Title: Conditional required field without the plugin
Last modified: February 12, 2018

---

# Conditional required field without the plugin

 *  Resolved [tiitus](https://wordpress.org/support/users/tiitus/)
 * (@tiitus)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/conditional-required-field-without-the-plugin/)
 * Hello,
 * I found this fab code on doing conditional required fields with a filter, without
   a plugin. However it is not working, and the form submits without the required
   fields. I am trying to make 2 fields required if a checkbox is checked, the “
   fromwho” and “friend-email”
    fields named respectively. I have also been trying
   to follow this example from [https://contactform7.com/2015/03/28/custom-validation/](https://contactform7.com/2015/03/28/custom-validation/)
 *     ```
       add_filter( 'wpcf7_validate_text', 'my_conditional_required', 10, 2 );
       add_filter( 'wpcf7_validate_text*', 'my_conditional_required', 10, 2 );
       add_filter( 'wpcf7_validate_email', 'my_conditional_required', 10, 2 );
       add_filter( 'wpcf7_validate_email*', 'my_conditional_required', 10, 2 );
   
       function my_conditional_required($result, $tag) {
           $tag = new WPCF7_Shortcode( $tag );
           $name = $tag->name;
   
           $value = isset( $_POST[$name] )
               ? trim( wp_unslash( strtr( (string) $_POST[$name], "\n", " " ) ) )
               : '';
   
       	if ( "fromwho" == $name && 'true' == $_POST['checkboxA'][0] ) :
               if ( '' == $value  ) :
                   $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );
               endif;
           endif;
   
       	if ( 'friend-email' == $name && 'true' == $_POST['checkboxA'][0] ) :
       		if ( '' == $value ) :
       			$result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );
       		endif;
       	endif;
   
           return $result;
       }
       ```
   
 * Any help appreciated!

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

 *  Plugin Author [Takayuki Miyoshi](https://wordpress.org/support/users/takayukister/)
 * (@takayukister)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/conditional-required-field-without-the-plugin/#post-9964778)
 * Can I see the site?
 *  Thread Starter [tiitus](https://wordpress.org/support/users/tiitus/)
 * (@tiitus)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/conditional-required-field-without-the-plugin/#post-9965103)
 * Hello Takayuki,
 * After some fiddling I actually remembered that my checkbox value is not true/
   false(oopsies). So I was just using the wrong value. Seems to be working correctly
   now that I got the checkbox value I was looking for.
    Great snippet of code (
   and even nicer with the custom validations provided by Contact Form 7)
 * 🙂

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

The topic ‘Conditional required field without the plugin’ 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/)

 * 2 replies
 * 2 participants
 * Last reply from: [tiitus](https://wordpress.org/support/users/tiitus/)
 * Last activity: [8 years, 4 months ago](https://wordpress.org/support/topic/conditional-required-field-without-the-plugin/#post-9965103)
 * Status: resolved