Form PHP Validation in Contact Form 7

0

I'm building a form in WordPress and need to validate bank details, CPF, Bank Number, Agency among others, and I use the Contact Form 7 plugin. My question is how do I validate the fields entered by the user in this plugin. The documentation and the FAQ of the plugin will not explain how to do this. Anyone have a suggestion?

    
asked by anonymous 01.09.2014 / 01:27

1 answer

3

It's not exactly simple ... You need to create another plugin to take care of validation. The first thing to do is to know which fields you want to filter:

Let'ssayit'sinthetypefieldsemail*,thenthefilterwillbewpcf7_validate_email*andtheerrormessagewillbeconnectedtothenameofthefieldyour-email.

Ifthesitehasmorethanoneformwithfieldsoftypeemail*itispossibletovalidateeachformbyfilteringthroughtheformID,whichisusedintheshortcode:[contact-form-7id="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 on) is:

Youhavea CF7 validation plugin with jQuery , but I do not know how it works. And remembering that it is only to turn off the JS in the browser to surpass this type of validation. See Can I validate only with jQuery or do I need PHP?

    
01.09.2014 / 03:23