WordPress.org

Support

Support » Plugins and Hacks » Custom validation on custom select don't work

Custom validation on custom select don't work

  • 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/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Takayuki Miyoshi

    @takayukister

    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);
    }

    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;
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom validation on custom select don't work’ is closed to new replies.