• Hi! I’m trying to implement a custom validation filter to customize the validation error message for “checkbox*” fields named “user-plan”. There’s only one condition to check, whether the field has been filled or not.

    The WP/PHP debugging gives no error, and the syntax is checked as correct, but once you try to send the e-mail, the validation and submitting process get stucked and the loader gif spins infinitely.

    Here’s the code:

    add_filter( 'wpcf7_validate_checkbox*', 'user_plan_validation_filter', 10, 2 );
    
    function user_plan_validation_filter( $result, $tag ) {
        $tag = new WPCF7_Shortcode( $tag );
     
        if ( $tag['name'] == 'user-plan' ) {
        	$user_plan = $_POST['user-plan'];
            // $user_plan = isset( $_POST['user-plan'] ) ? trim( $_POST['user-plan'] ) : '';
     
            if ( $user_plan == '' ) {
                $result->invalidate( $tag, "Selecciona un plan para poder finalizar el tramite" );
            }
        }
     
        return $result;
    }
    • This topic was modified 8 years, 4 months ago by maehdros.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    Check your PHP error log.

    Thread Starter maehdros

    (@maehdros)

    Hi! Takayuki, thanks for your excellent work with Contact Form 7, it’s a tremendous helper in my jobs.

    Here’s the error logged:

    [Fatal error] => 09.11.2016 16:47:20
    Cannot use object of type WPCF7_Shortcode as array
    in /home/mcmovil/public_html/wp-content/themes/mcmovil-theme/functions.php (209)

    The “line 209” is this:

    if ( $tag['name'] == 'user-plan' ) {

    Cheers!

    • This reply was modified 8 years, 4 months ago by maehdros.
    Plugin Author Takayuki Miyoshi

    (@takayukister)

    Yes, you are using a WPCF7_Shortcode object ($tag) as an array there. You can’t do that.

    Thread Starter maehdros

    (@maehdros)

    Got it! That bug is solved. Here’s the code:

    add_filter( 'wpcf7_validate_checkbox*', 'user_plan_validation_filter', 10, 2 );
    
    function user_plan_validation_filter( $result, $tag ) {
        $tag = new WPCF7_Shortcode( $tag );
     
        if ( $tag->name == 'user-plan' ) {
        	$user_plan = $_POST['user-plan'];
            // $user_plan = isset( $_POST['user-plan'] ) ? trim( $_POST['user-plan'] ) : '';
     
            if ( $user_plan == '' ) {
                $result->invalidate( $tag, "Selecciona un plan para poder finalizar el tramite" );
            }
        }
     
        return $result;
    }

    Now the validation process doesn’t get stuck, but the validation message shown is not my custom one but the one set in the form options. What can I be doing wrong?

    Thanks a lot for your help Takayuki, cheers!

    Thread Starter maehdros

    (@maehdros)

    Some debug information I pulled out from “debug.log”:

    [10-Nov-2016 17:09:58 UTC] PHP Notice: Undefined index: user-plan in /home/mcmovil/public_html/wp-content/themes/mcmovil-theme/functions.php on line 212

    Line 212 is this:

    $user_plan = $_POST['user-plan'];

    Sorry for the commented line in the previous post code, I forgot to clean it.

    Thread Starter maehdros

    (@maehdros)

    Well, at this point, I’ve arrived to a code that is working without bugs, but it keeps failing in its purpose:

    add_filter( 'wpcf7_validate_checkbox*', 'user_plan_validation_filter', 20, 2 );
    
    function user_plan_validation_filter( $result, $tag ) {
    	$tag = new WPCF7_Shortcode( $tag );
    
    	$field = 'user-plan';
    
    	if ( $field == $tag->name ):
    		if ( isset( $_POST['user-plan'] ) ):
    			$user_plan = trim( $_POST['user-plan'] );
    		else:
    			$user_plan = '';
    		endif;
    
    		if ( $user_plan == '' ):
    			$result->invalidate( $tag, "Selecciona un plan para poder finalizar el tramite" );
    		endif;
    	endif;
     
    	return $result;
    }

    Using the PHP Error Log I checked that all the variables are being filled with the values I expect, the conditional sentences also are working as expected, but the validation message keeps showing the one setted in the Contact Form configuration.

    Any help?

    Thanks!

    Hi maehdros,

    I exactly have the same problem as you, i.e., my filter is working fine but the validation message keeps showing the one set as default and not my custom one. Did you find any solution for this?

    Thanks a lot!
    Nima

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Custom validation filter stucks form validation’ is closed to new replies.