• Resolved foscolino

    (@foscolino)


    Hi, i have create a custom add shortcode for extract field from db table by select box.
    All work ok, but Now i don’t find a solution to validate this field.

    Any ideas?

    wpcf7_add_shortcode('postdropdown', 'createbox', true);
    function createbox(){
    global $wpdb;
    $campocustom = $wpdb->get_results( "SELECT * FROM db_ippbrescia.wpipp_ai1ec_events WHERE contact_name IS NOT NULL AND contact_name != '';" );
    $output = "<span class='wpcf7-form-control-wrap cursus'><select name='cursus' class='wpcf7-form-control wpcf7-select wpcf7-validates-as-required' aria-required='true' id='cursus' onchange='document.getElementById(\"cursus\").value=this.value;'><option>---</option><span>";
    foreach ( $campocustom as $custom ) : setup_postdata($custom);
    /**     echo $custom->contact_name; **/
            $title = ($custom->contact_name);
            $output .= "<option value='$title'> $title </option>";
    endforeach;
    $output .= "</select>";
    return $output;
    }

    i have added this code but not work

    add_filter( 'wpcf7_validate_postdropdown', 'your_validation_filter_func', 10, 2 );
    add_filter( 'wpcf7_validate_postdropdown*', 'your_validation_filter_func', 10, 2 );
    
    function your_validation_filter_func( $result, $tag ) {
    	$type = $tag['type'];
    	$name = $tag['name'];
    
    	if ( 'postdropdown' == $name ) {
    		$the_value = $_POST[$name];
    
    		if ( is_not_valid_against_your_validation( $the_value ) ) {
    			$result['valid'] = false;
    			$result['reason'][$name] = "Error message here";
    		}
    	}
    
    	return $result;
    }

    https://www.ads-software.com/plugins/contact-form-7/

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

    (@takayukister)

    $output = "<span class='wpcf7-form-control-wrap cursus'><select name='cursus' class='wpcf7-form-control wpcf7-select wpcf7-validates-as-required' aria-required='true' id='cursus' onchange='document.getElementById(\"cursus\").value=this.value;'><option>---</option><span>";

    Because the name of the select box is “cursus”, not “postdropdown”.

    if ( 'postdropdown' == $name ) {
    		$the_value = $_POST[$name];
    Thread Starter foscolino

    (@foscolino)

    Hi Miyoshi, i have tried to change code to “cursus”, but when i try to send form I get a loop and the email is not sent ??

    if ( 'cursus' == $name ) {
    		$the_value = $_POST[$name];

    I have set my php.ini in debug mode but i don’t see any errors ??

    this is apache log:

    [Sat Jan 10 09:39:42 2015] [error] [client 192.168.20.40] PHP Fatal error: Call to undefined function is_not_valid_against_your_validation()

    this is my error

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    So where have you defined this is_not_valid_against_your_validation()?

    Thread Starter foscolino

    (@foscolino)

    mmm…I have not set any function about “is_not_valid_against_your_validation” ??

    I would not know how to do ??

    how can I do to make my custom select like any other form?

    I would like the validation functions like other form, how this example

    thanks a lot for your support.

    Thread Starter foscolino

    (@foscolino)

    any idea for validation? ??

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    So in what cases an user input “is not valid against your validation”? It’s up to you.

    Thread Starter foscolino

    (@foscolino)

    “is not valid against your validation” when default value of custom select form is “—“.
    VIEW

    I can’t write a correct function ??

    sorry but $output should be:

    $output = "<span class='wpcf7-form-control-wrap cursus'><select name='cursus' class='wpcf7-form-control wpcf7-select wpcf7-validates-as-required' aria-required='true' id='cursus' onchange='document.getElementById(\"cursus\").value=this.value;'><option>---</option><span>";

    or

    $output = "<span class='is not valid against your validation cursus'><select name='cursus' class='wpcf7-form-control wpcf7-select wpcf7-validates-as-required' aria-required='true' id='cursus' onchange='document.getElementById(\"cursus\").value=this.value;'><option>---</option><span>";

    Thread Starter foscolino

    (@foscolino)

    Hi, i have resolved the problem, the validation function now is:

    function filter_cursusecm($result,$tag){
            $type = $tag['type'];
            $name = $tag['name'];
            if($name == 'cursusecm'){
                    $the_value = $_POST[$name];
                    if(!is_cursusecm_blank($the_value)){
                            $result['valid'] = false;
                            $result['reason'][$name] = '<br>---------------------------------------------------------------------------------------------<br>Attenzione! Non hai indicato il Corso al quale ti vuoi iscrivere. Grazie<br>---------------------------------------------------------------------------------------------';
                    }
            }
            return $result;
    }
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Validate field select box custom’ is closed to new replies.