Form PHP validation in Contact Form 7

Asked

Viewed 2,279 times

0

I am building a form in Wordpress and need to validate banking data, CPF, Bank Number, Agency among others, and use the plugin Contact Form 7. My question is how to validate by PHP the fields typed by the user in this plugin. To documentation and the FAQ plugin does not explain how to do this. Someone has a suggestion?

1 answer

3

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

configuração do cf7

Let’s say it’s in the type fields 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 the 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:  /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 turned on) is:



Has a CF7 validation plugin with jQuery, but I don’t know how it works. And remember that just turn off the JS in the browser to override this type of validation. See Can I validate only with jQuery or do I need PHP?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.