PHP form validation in Contact form 7

I am setting up a form in WordPress and I need to validate bank details, CPF, Bank Number, Agency among others, and I use the Contact Form 7 plugin. My doubt is how do I validate by PHP the Fields entered by the user in this plugin. The documentation and the FAQ of the plugin do not explain how to do this. Does anyone have a suggestion?

Author: brasofilo, 2014-08-31

1 answers

Is not exactly simple... It is necessary to create another plugin to take care of the validation. The first thing to do is know which fields you want to filter:

cf7 configuration

Let's say it is in the fields of type email*, then the filter will be wpcf7_validate_email* and the error message will be connected to the field name your-email.

If the site has more than one form with fields of type email* it is possible to validate each form by filtering through the form ID, which is used in the shortcode: [contact-form-7 id="2919" title="aaa"].

The plugin code would be:

<?php
/**
 * Plugin Name: (SOPT) Validar forms do CF7
 * Plugin URI:  http://pt.stackoverflow.com/a/31028/201
 * Author:      brasofilo 
 */

/**
 * Evita ativar o plugin se o CF7 não estiver ativo
 */
register_activation_hook( __FILE__, 'activation_sopt_31022' );
function activation_sopt_31022()
{
    $plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
    check_admin_referer( "activate-plugin_{$plugin}" ); 
    if ( ! class_exists( 'WPCF7_Submission') )
        wp_die( 'O Contact Form 7 não está ativo' );
}

add_filter( 'wpcf7_posted_data', 'check_form_so_31022' );

/**
 * Iniciar a checagem dos forms enviados
 */
function check_form_so_31022( $data )
{
    # Conferir ID de um form específico
    if( $data['_wpcf7'] !== '2919' )
        return;

    # Validar campo de email
    if( !validate_email_so_31022( $data['your-email'] ) )
        add_filter( 'wpcf7_validate_email*', 'erro_email_so_31022', 10, 2 );

    # Validar campo de texto
    if( !validate_text_so_31022( $data['your-subject'] ) )
        add_filter( 'wpcf7_validate_text', 'erro_text_so_31022', 10, 2 );

    return $data;
}

/**
 * TODO: Função personalizada para validar texto
 */
function validate_text_so_31022( $text )
{
    # FAÇA AQUI SUA VALIDAÇÃO e retorne true ou false
    return false;
}

/**
 * Filtro específico para mostrar erro em campo de texto
 */
function erro_text_so_31022( $result, $tag )
{
    $result['valid'] = false;
    $result['reason']['your-subject'] = 'Erro';
    return $result;

}

/**
 * TODO: Função personalizada para validar email
 */
function validate_email_so_31022( $email )
{
    # FAÇA AQUI SUA VALIDAÇÃO e retorne true ou false
    return false;
}

/**
 * Filtro específico para mostrar erro em campo de email
 */
function erro_email_so_31022( $result, $tag )
{
    $result['valid'] = false;
    $result['reason']['your-email'] = 'Logical Error: Check in date should be before check out date';
    return $result;

}

The result when trying to submit the form (with AJAX attached) is:



Has a plugin for cf7 validation with jQuery , but I don't know how it works. And remembering that just turn off the JS in the browser to overcome this type of validation. Empty Can I validate only with jQuery or do I need PHP?

 3
Author: brasofilo, 2017-05-23 12:37:26