Contact Form 7 Error Message

User
change contact form 7 empty requred filed error message after submit
Assistant
You can change the error message for empty required fields in **Contact Form 7** by using the following method:

### 1. Modify Messages in Contact Form 7 Settings
1. Go to **WordPress Admin Dashboard**.
2. Navigate to **Contact β†’ Contact Forms**.
3. Edit the form you want to modify.
4. Scroll down to the **Messages** section.
5. Find the **"The field is required."** message and change it to your preferred text.
6. Click **Save**.

### 2. Customize Error Message Using Code (Optional)
If you want to change the message dynamically using a filter, add the following code to your **theme’s functions.php** file:

add_filter('wpcf7_validate', 'custom_cf7_error_messages', 10, 2);
function custom_cf7_error_messages($result, $tag) {
    $name = $tag['name'];
    if ($result->has_error($name)) {
        $result->invalidate($name, "Please fill out this field.");
    }
    return $result;
}

Replace `"Please fill out this field."` with your custom message.

Let me know if you need further modifications! πŸš€

User
remove_action('wpcf7_swv_create_schema', 'wpcf7_swv_add_text_rules', 10);
remove_action('wpcf7_swv_create_schema', 'wpcf7_swv_add_textarea_rules', 10);

// Adding the customized validation rules for text fields and textareas in Contact Form 7
add_action(
'wpcf7_swv_create_schema',
function ( $schema, $contact_form ) {
// Transfer of the existing fields in the form
$tags = $contact_form->scan_form_tags();

foreach ( $tags as $tag ) {
// Initialize the standard error message
$error_message = wpcf7_get_message( 'invalid_required' );

// Customize the error messages for certain fields
if ( $tag->is_required() ) {
if ($tag->name == 'tel-84') {
$error_message = 'Please enter your tel.';
} elseif ($tag->name == 'your-subject') {
$error_message = 'Please enter your e-mail address.';
}

// Adding the rule
$schema->add_rule(
wpcf7_swv_create_rule( 'required', array(
'field' => $tag->name,
'error' => $error_message,
) )
);
}

// Continue to use the original validation rules for e-mail, URL, telephone, min/max length
if ( 'email' === $tag->basetype && $tag->is_required() ) {
$schema->add_rule(
wpcf7_swv_create_rule( 'email', array(
'field' => $tag->name,
'error' => wpcf7_get_message( 'invalid_email' ),
) )
);
}

if ( 'url' === $tag->basetype && $tag->is_required() ) {
$schema->add_rule(
wpcf7_swv_create_rule( 'url', array(
'field' => $tag->name,
'error' => wpcf7_get_message( 'invalid_url' ),
) )
);
}

if ( 'tel' === $tag->basetype && $tag->is_required() ) {
$schema->add_rule(
wpcf7_swv_create_rule( 'tel', array(
'field' => $tag->name,
'error' => wpcf7_get_message( 'invalid_tel' ),
) )
);
}

if ($minlength = $tag->get_minlength_option()) {
$schema->add_rule(
wpcf7_swv_create_rule( 'minlength', array(
'field' => $tag->name,
'threshold' => absint($minlength),
'error' => wpcf7_get_message('invalid_too_short'),
) )
);
}

if ($maxlength = $tag->get_maxlength_option()) {
$schema->add_rule(
wpcf7_swv_create_rule( 'maxlength', array(
'field' => $tag->name,
'threshold' => absint($maxlength),
'error' => wpcf7_get_message('invalid_too_long'),
) )
);
}
}
},
10, 2
);
above not working

Assistant
Your custom validation logic for Contact Form 7 using **`wpcf7_swv_create_schema`** is not working likely because CF7's newer versions use a different validation mechanism, or the function names and hooks might not be triggering correctly.

### Alternative Approach:
Instead of relying on **`wpcf7_swv_create_schema`**, try the **`wpcf7_validate_*`** filters to customize validation messages.

#### βœ… **Solution: Use Custom Validation Filters**
Modify the required field error messages using the `wpcf7_validate` filter in your `functions.php` file:

add_filter('wpcf7_validate_text', 'custom_cf7_text_validation', 10, 2);
add_filter('wpcf7_validate_text*', 'custom_cf7_text_validation', 10, 2);
add_filter('wpcf7_validate_tel', 'custom_cf7_tel_validation', 10, 2);
add_filter('wpcf7_validate_tel*', 'custom_cf7_tel_validation', 10, 2);
add_filter('wpcf7_validate_email', 'custom_cf7_email_validation', 10, 2);
add_filter('wpcf7_validate_email*', 'custom_cf7_email_validation', 10, 2);

function custom_cf7_text_validation($result, $tag) {
    $name = $tag->name;
    
    if (isset($_POST[$name]) && empty(trim($_POST[$name]))) {
        if ($name == 'tel-84') {
            $result->invalidate($tag, 'Please enter your telephone number.');
        } elseif ($name == 'your-subject') {
            $result->invalidate($tag, 'Please enter your subject.');
        } else {
            $result->invalidate($tag, 'This field cannot be empty.');
        }
    }

    return $result;
}

function custom_cf7_tel_validation($result, $tag) {
    $name = $tag->name;

    if (isset($_POST[$name]) && empty(trim($_POST[$name]))) {
        $result->invalidate($tag, 'Please enter a valid phone number.');
    }

    return $result;
}

function custom_cf7_email_validation($result, $tag) {
    $name = $tag->name;

    if (isset($_POST[$name]) && empty(trim($_POST[$name]))) {
        $result->invalidate($tag, 'Please enter a valid email address.');
    }

    return $result;
}

### πŸ”Ή **Why This Works**
1. **Uses `wpcf7_validate_*` filters** – ensures CF7 processes custom validation.
2. **Applies specific error messages** to fields like `tel-84` and `your-subject`.
3. **Works for required fields (`*` in the CF7 form tags)**.

Try this and let me know if you need further adjustments! πŸš€

Leave a Comment

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