WordPress.org

Forums

Contact Form 7
Custom validation on custom select don't work (3 posts)

  1. ipla
    Member
    Posted 1 year ago #

    Hi!

    I'm working on a site and I need a custom select for "Centers" pages in WordPress Database. I manage to do that, but I also need this select to be mandatory, so when I create the custom validation function, it doesn't work. I suspect i'ts not even going through that function. Anyone knows why?

    /* "Centro" dropdown - get "centros" and construct select */
    
    wpcf7_add_shortcode('centerdropdown', 'wpcf7_centerdropdown_shortcode', true);
    wpcf7_add_shortcode('centerdropdown*', 'wpcf7_centerdropdown_shortcode', true);
    
    function wpcf7_centerdropdown_shortcode($tag){ 
    
    	global $post;
    	$args = array( 'post_type' => 'centers', 'posts_per_page' => 100 );
    	$centers = get_posts( $args );
    	$output = '<select name="centerdropdown" class="centerdropdown form-control wpcf7-select wpcf7-form-control wpcf7-validates-as-required">';
    	$output .= '<option value="notselected">Centro</option>';
    	foreach ( $centers as $center ):
    		$output .= "<option data-email='".get_post_meta( $center->ID, 'center_email', true )."' value='";
    		$output	.= get_the_title($center);
    		$output .= "'>".get_the_title($center)."</option>";
    	endforeach;
    	$output .= "</select>";
    	return $output;
    }
    
    /* "Centro" dropdown - validation filter */
    
    add_filter('wpcf7_centerdropdown', 'wpcf7_centerdropdown_filter', 10, 2);
    add_filter('wpcf7_centerdropdown*', 'wpcf7_centerdropdown_filter', 10, 2);
    
    function wpcf7_centerdropdown_filter( $result, $tag ) {
    
    	$value = $tag['type'];
    	$name = $tag['name'];
    
    	if($name == 'centerdropdown'){
    		$the_value = $_POST[$name];
    		if($the_value == 'notselected'){
    			$result['valid'] = false;
    			$result['reason'][$name] = wpcf7_get_message( 'invalid_required' );
    		}
    	}
    
    	return $result;
    }

    https://wordpress.org/plugins/contact-form-7/

  2. Takayuki Miyoshi
    Member
    Plugin Author

    Posted 1 year ago #

    Not sure where you have put this code in, but maybe that should be:

    add_action( 'wpcf7_init', 'iplas_custom_wpcf7_shortcode_function' );
    
    function iplas_custom_wpcf7_shortcode_function() {
    	wpcf7_add_shortcode('centerdropdown', 'wpcf7_centerdropdown_shortcode', true);
    	wpcf7_add_shortcode('centerdropdown*', 'wpcf7_centerdropdown_shortcode', true);
    }
  3. ipla
    Member
    Posted 1 year ago #

    I've add these lines to my code and it's not working. I've add an echo to validation function to debug and it not appears on form submit, so the plugin is not going through that function. Any idea?

    Now, the code is:

    /* "Centro" dropdown */
    
    add_action( 'wpcf7_init', 'new_custom_wpcf7_shortcode_function' );
    
    function new_custom_wpcf7_shortcode_function() {
    	wpcf7_add_shortcode('centerdropdown', 'wpcf7_centerdropdown_shortcode', true);
    	wpcf7_add_shortcode('centerdropdown*', 'wpcf7_centerdropdown_shortcode', true);
    }
    
    /* "Centro" dropdown - get "centros" and construct select */
    
    wpcf7_add_shortcode('centerdropdown', 'wpcf7_centerdropdown_shortcode', true);
    wpcf7_add_shortcode('centerdropdown*', 'wpcf7_centerdropdown_shortcode', true);
    
    function wpcf7_centerdropdown_shortcode($tag){ 
    
    	global $post;
    	$args = array( 'post_type' => 'centers', 'posts_per_page' => 100 );
    	$centers = get_posts( $args );
    	$output = '<select name="centerdropdown" class="centerdropdown form-control wpcf7-select wpcf7-form-control wpcf7-validates-as-required">';
    	$output .= '<option value="notselected">Centro</option>';
    	foreach ( $centers as $center ):
    		$output .= "<option data-email='".get_post_meta( $center->ID, 'center_email', true )."' value='";
    		$output	.= get_the_title($center);
    		$output .= "'>".get_the_title($center)."</option>";
    	endforeach;
    	$output .= "</select>";
    	return $output;
    }
    
    /* "Centro" dropdown - validation filter */
    
    add_filter('wpcf7_centerdropdown', 'wpcf7_centerdropdown_filter', 10, 2);
    add_filter('wpcf7_centerdropdown*', 'wpcf7_centerdropdown_filter', 10, 2);
    
    function wpcf7_centerdropdown_filter( $result, $tag ) {
    	echo '<h1>GOING THROUGH VALIDATION FUNCTION</h1>';
    
    	$value = $tag['type'];
    	$name = $tag['name'];
    
    	if($name == 'centerdropdown'){
    		$the_value = $_POST[$name];
    		if($the_value == 'notselected'){
    			$result['valid'] = false;
    			$result['reason'][$name] = wpcf7_get_message( 'invalid_required' );
    		}
    	}
    
    	return $result;
    }

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Contact Form 7
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic