• Hello there I was able to implement a custom shortcode and field in CF7 in order to get a list of the last 24 month of the year in italian language on a select input box.

    Non my last point is how to validate the field in case empty and not selected???

    Please help

    Here is my script:

    <?php
    
    /* get months in php */
    
     function mesi_anno2() {
     $mese = array ("Gennaio-", "Febbraio-", "Marzo-", "Aprile-", "Maggio-", "Giugno-", "Luglio-", "Agosto-", "Settembre-", "Ottobre-", "Novembre-", "Dicembre-");
     $str="";
            for($i = 0; $i < 24; $i++) {
            $time = mktime(0, 0, 0, date('n') - $i);
            $str .="<option value=".$mese[(date("n", $time)-1)].date("Y", $time).">".$mese[(date("n", $time)-1)].date("Y", $time)."</option>";
        }
      return $str;
       } 
    
    /* shortcode */
    
    wpcf7_add_shortcode('custom_date',  'wpcf7_custom_date_shortcode_handler', true);
    wpcf7_add_shortcode('custom_date*', 'wpcf7_custom_date_shortcode_handler', true);
    
    function wpcf7_custom_date_shortcode_handler($tag) {
    
        if (!is_array($tag)) return '';
    
        $name = $tag['name'];
        if (empty($name)) return '';
    
    	$validation_error = wpcf7_get_validation_error( $tag->name );
    
    	$class = wpcf7_form_controls_class( $tag->type );
    
    	if ( $validation_error )
    		$class .= ' wpcf7-not-valid';
    
          $html = '
    	  <select name="'.$name.'">
     	  <option value="">---</option>
     	  '.mesi_anno2().'
     	  </select>';
    
    return $html;
    }
    
    add_filter('wpcf7_custom_date',  'custom_date_filter', 10, 2);
    add_filter('wpcf7_custom_date*', 'custom_date_filter', 10, 2);  
    
    function wpcf7_custom_date_validation_filter( $result, $tag ) {
    	$tag = new WPCF7_Shortcode( $tag );
    
    	$name = $tag->name;
    
    	if ( isset( $_POST[$name] ) && is_array( $_POST[$name] ) ) {
    		foreach ( $_POST[$name] as $key => $value ) {
    			if ( '' === $value )
    				unset( $_POST[$name][$key] );
    		}
    	}
    
    	if ( $tag->is_required() ) {
    		if ( ! isset( $_POST[$name] )
    		|| empty( $_POST[$name] ) && '0' !== $_POST[$name] ) {
    			$result['valid'] = false;
    			$result['reason'][$name] = 'ERRORE';
    		}
    	}
    
    	return $result;
    }
    
    ?>

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

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

    (@takayukister)

    add_filter('wpcf7_custom_date',  'custom_date_filter', 10, 2);
    add_filter('wpcf7_custom_date*', 'custom_date_filter', 10, 2);

    These filter names should be wpcf7_validate_custom_date and wpcf7_validate_custom_date* respectively.

    Thread Starter Corrado Prever

    (@artphotoasia)

    Thanks for the answer… as you can see in my code I have already inserted the lines.

    add_filter('wpcf7_custom_date',  'custom_date_filter', 10, 2);
    add_filter('wpcf7_custom_date*', 'custom_date_filter', 10, 2);

    why is not working?

    Thread Starter Corrado Prever

    (@artphotoasia)

    I noted an error and make a correction but still does not work ….

    Here my cf7 custom module

    <?php
    
    /* parte per trovare i mesi in italiano in php */
    
     function mesi_anno2() {
     $mese = array ("Gennaio-", "Febbraio-", "Marzo-", "Aprile-", "Maggio-", "Giugno-", "Luglio-", "Agosto-", "Settembre-", "Ottobre-", "Novembre-", "Dicembre-");
     $str="";
            for($i = 0; $i < 24; $i++) {
            $time = mktime(0, 0, 0, date('n') - $i);
            $str .="<option value=".$mese[(date("n", $time)-1)].date("Y", $time).">".$mese[(date("n", $time)-1)].date("Y", $time)."</option>";
        }
      return $str;
       } 
    
    /* parte per inserire lo shortcode */
    
    wpcf7_add_shortcode('custom_date',  'wpcf7_custom_date_shortcode_handler', true);
    wpcf7_add_shortcode('custom_date*', 'wpcf7_custom_date_shortcode_handler', true);
    
    function wpcf7_custom_date_shortcode_handler($tag) {
    
        if (!is_array($tag)) return '';
    
        $name = $tag['name'];
        if (empty($name)) return '';
    
    	$validation_error = wpcf7_get_validation_error( $tag->name );
    
    	$class = wpcf7_form_controls_class( $tag->type );
    
    	if ( $validation_error )
    		$class .= ' wpcf7-not-valid';
    
          $html = '
    	  <select name="'.$name.'">
     	  <option value="">---</option>
     	  '.mesi_anno2().'
     	  </select>';
    
    return $html;
    }
    
    add_filter('wpcf7_custom_date',  'custom_date_filter', 10, 2);
    add_filter('wpcf7_custom_date*', 'custom_date_filter', 10, 2);  
    
     function wpcf7_custom_date_filter( $result, $tag )
    
    { 
    
    	$tag = new WPCF7_Shortcode( $tag );
     	$name = $tag->name;
     	$value = isset( $_POST[$name] );
    
    	if ( 'custom_date*' == $tag->type ) {
    		if ( '' == $value ) {
    			$result['valid'] = false;
    			$result['reason'][$name] = wpcf7_get_message( 'invalid_required' );
    		}
    	}
    
    	return $result;
    }
    
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘validation issue in custom field and custom shortcode’ is closed to new replies.