validation issue in custom field and custom shortcode
-
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; } ?>
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘validation issue in custom field and custom shortcode’ is closed to new replies.