In this guide, you will learn how to create custom validation in CF7. This guide is also the solution for programmatically making fields required.

Contact Form 7 uses the validation server-side, which means we need to hook into the PHP functions/filters in order to create custom validations.

In my scenario, I have a select field called type-of-enquiry with 2 options, Sales and Support.

If sales is selected, I am showing and validating country(select) and state(select), if support is selected, I am showing product(select), telephone(tel) and subject(text).

We start off by creating a custom file in our theme or child theme, I’ve named mine “cf7-custom-validation.php” and saved it in a custom folder called “inc” and we import it in our functions.php file using:

functions.php

require_once( get_template_directory() . '/inc/cf7-custom-validation.php' );
Code language: PHP (php)

cf7-custom-validation.php

<?php // Block direct access to the file defined( 'ABSPATH' ) || exit; /** * Each field type has its own validation filter, * so we create a function for each type */ add_filter( 'wpcf7_validate_select', 'wpharvest_custom_validation_select', 20, 2 ); add_filter( 'wpcf7_validate_tel', 'wpharvest_custom_validation_tel', 20, 2 ); add_filter( 'wpcf7_validate_text', 'wpharvest_custom_validation_text', 20, 2 ); /** * Go through all the select fields */ function wpharvest_custom_validation_select( $result, $tag ) { // Get the type of enquiry from the dropdown $enquiryType = isset( $_POST['type-of-enquiry'] ) ? trim( $_POST['type-of-enquiry'] ) : ''; // If selection is Sales if( $enquiryType == 'Sales' ){ // If current validation is your-country and is empty then invalidate field if ( 'your-country' == $tag->name && empty( $_POST['your-country'] ) ) { $result->invalidate($tag, "This field is required."); } // We can also make validation dependent on other fields // If current validation is state, the country selected is United States and is empty then invalidate field if ( 'state' == $tag->name && trim( $_POST['your-country'] ) == 'United States' && empty( $_POST['state'] ) ) { $result->invalidate($tag, "This field is required."); } } // If selection is Support if( $enquiryType == 'Support' ){ // If current validation is product and is empty then invalidate field if ( 'product' == $tag->name && empty( $_POST['product'] ) ) { $result->invalidate($tag, "This field is required."); } } return $result; } /** * Go through all the tel fields */ function wpharvest_custom_validation_tel( $result, $tag ) { // Get the type of enquiry from the dropdown $enquiryType = isset( $_POST['type-of-enquiry'] ) ? trim( $_POST['type-of-enquiry'] ) : ''; // If selection is Support if( $enquiryType == 'Support' ){ // If current validation is telephone and is empty then invalidate field if ( 'telephone' == $tag->name && empty( $_POST['telephone'] ) ) { $result->invalidate($tag, "This field is required."); } } return $result; } /** * Go through all the text fields */ function wpharvest_custom_validation_text( $result, $tag ) { // Get the type of enquiry from the dropdown $enquiryType = isset( $_POST['type-of-enquiry'] ) ? trim( $_POST['type-of-enquiry'] ) : ''; // If selection is Support if( $enquiryType == 'Support' ){ // If current validation is subject and is empty then invalidate field if ( 'subject' == $tag->name && empty( $_POST['subject'] ) ) { $result->invalidate($tag, "This field is required."); } } return $result; }
Code language: HTML, XML (xml)

Published by Dragos Micu

WooCommerce

2 Comments

  1. You can also try one plugin that allow to set custom validation message for every field.
    Plugin URL: https://wordpress.org/plugins/cf7-custom-validation-message/
    Thanks for your code it works as well..

    Reply
  2. Hey Dragos,

    I have an issue that´s similar but not as easy as that the “normal” fields.

    I have an issue with the file fields.

    Bild-Upload*
    [file* lh-img filetypes:jpg|png|jpeg class:lh-img-input]
    Dateien durchsuchen

    Video-Upload*
    [file* lh-video filetypes:mp4 class:lh-video-input]
    Dateien durchsuchen

    That´s them, so you can see there is one for a video file and one for an image.

    So when you upload a file to “lh-img”, the field “lh-video” shouldn´t be required anymore, same goes for the reversed case (video uploaded, img not required).

    Your code works for fields like mail or text, but the file fields are another thing there and I couldn´t figure it out. Maybe someone has the same Problem and that would help them as well.

    Reply

Leave a Reply

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

experience the ultimate intimacy with cbd gummies 300mg for sex unlocking a new level of pleasure a do love bites gummies have cbd in them unlock the secrets to a calmer you with the best cbd gummies finding the best men 39 s gummy vitamins for optimal health 54577 achieve unparalleled bliss with bliss bites cbd gummies for ed a comprehensive review say goodnight to stubborn fat the science behind nighttime weight loss gummies the ultimate guide to side effects for keto gummies separating fact from fiction cbd gummies after bariatric surgery a game changing approach to recovery and weight management kim kardashian weight loss did the reality star use ozempic for her slimdown what are the best keto gummies discover the top rated delicious and effective options for a keto kelly clarkson weight loss keto gummies unlocking a slimmer healthier you with these game changing does cbd show up in urine a comprehensive guide to detection and drug tests cbd gummies vs cbd oils which is the best choice for your wellness needs calmwell cbd gummies for neuropathy a comprehensive guide to natural relief can you take cbd thc gummies on a plane a comprehensive guide to air travel with cbd edibles 37696 finding the best sativa cbd gummies near me a comprehensive guide to relaxation and wellness qzgkp experience the bliss of makers cbd gummies hemp extract a comprehensive guide to natural wellness enhancing fitness routines with cbd workout gummies in miami a comprehensive guide 92644 experience the power of cbd gummies for high blood pressure for sale a comprehensive guide experience the bliss of cbd gummies del doctor juan a comprehensive review of the best cbd products alpha labs cbd gummies reviews thc free 100 legit most effective amp powerful cbd