- Home
- Blog
- Contact
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)