• Resolved Comaris GbR

    (@websanityofficial)


    Hi there, thanks for this awesome plugin!

    I would like to replace the default “This field is required.” validation error message with individual messages determined by the specific field. So, for example

    the field “your-name” returns the error “Please enter your name.“,
    the field “your-email” returns “Please enter your e-mail.
    and the field “your-message” returns “Let us know, how we can help you.

    Therefore, I wrote this code:

    add_filter('wpcf7_validate_text*', 'custom_cf7_validation_filter', 20, 2);
    add_filter('wpcf7_validate_email*', 'custom_cf7_validation_filter', 20, 2);
    add_filter('wpcf7_validate_textarea*', 'custom_cf7_validation_filter', 20, 2);
    
    function custom_cf7_validation_filter($result, $tag) {
        $name = $tag->name;
    
        switch ($name) {
            case 'your-name':
                if (empty($_POST['your-name'])) {
                    $result->invalidate($tag, "Please enter your name.");
                }
                break;
            case 'your-email':
                if (empty($_POST['your-email'])) {
                    $result->invalidate($tag, "Please enter your e-mail.");
                }
                break;
            case 'your-message':
                if (empty($_POST['your-message'])) {
                    $result->invalidate($tag, "Let us know, how we can help you.");
                }
                break;
        }
    
        return $result;
    }

    The code works, but only if no other default error message is returned. Therefore I would need to disable the default “This field is required.” message or have a way to override it. Is this possible? Thanks in advance.

    EDIT: To make the problem visible on the provided URL, I added a Test-Field with an always returning error message “Test error: This error is always displayed.” Code:

    case 'test-feld-zum-testen':
                $result->invalidate($tag, "Test error: This error is always displayed.");
                break;

    If you leave every field blank and click send, the “This field is required.” error gets displayed. If you now enter something in the test field, the “alwas displayed” error message gets displayed.

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    The current Contact Form 7 uses Schema-Woven Validation, not the classic filter-based validation, for user inputs. If you want to customize the error messages, once call remove_action() to remove the default action functions, and re-call add_action() to declare your custom rules.

    Thread Starter Comaris GbR

    (@websanityofficial)

    Excellent, thank you very much!

    I removed the two validation functions and added my own one with custom conditions for certain field names. Works like a charm!

    For anyone interested, find the code for the functions.php below:

    // Removing the default validation rules for text fields and textareas in Contact Form 7
    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 == 'your-name') {
                        $error_message = 'Please enter your name.';
                    } elseif ($tag->name == 'your-email') {
                        $error_message = 'Please enter your e-mail address.';
                    } elseif ($tag->name == 'your-message' && $tag->basetype == 'textarea') {
                        $error_message = 'Please let us know how we can help you.';
                    }
    
                    // 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
    );
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Replace default validation error message with individual messages’ is closed to new replies.