• Resolved Olivier Raimbaud

    (@oraimbaud)


    Hi, PHP newbie here.
    I created a custom tag [jobs*] which displays a select element containing a list of custom posts. I wanted it to be validated on submit as the following official tutorial here.

    function custom_select_confirmation_validation_filter( $result, $tag ) {
      if ( 'custom-select' == $tag->name ) {
        $test_custom_select = $_POST['custom-select'];
    
        if ( empty( $test_custom_select ) ) {
          $result->invalidate( $tag, 'This field is required.' );
        }
      }
      return $result;
    }
    add_filter( 'wpcf7_validate_jobs*', 'custom_select_confirmation_validation_filter', 20, 2 );

    Since I updated to cf7 v.5.0 that’s not working anymore, unless I’m missing something. Help is much appreciated!

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

    (@takayukister)

    Where can we see the site?

    Thread Starter Olivier Raimbaud

    (@oraimbaud)

    Hi, thank you for replying. You can see the site here

    Here are my two functions:

    // Contact Form 7 custom tag //
    
    function custom_form_tag_angebote() {
      wpcf7_add_form_tag( 'angebote*', 'custom_select_handler', array( 'name-attr' => true ) );
    }
    
    add_action( 'wpcf7_init', 'custom_form_tag_angebote' );
    
    function custom_select_handler( $tag ) {
      global $post;
      $page_slug = $post->post_name;
    
      if ( is_archive() ) {
        $terms = get_the_terms( get_the_ID(), 'stellen' );
    
        foreach ( $terms as $term ) {
          $page_slug = $term->slug;
        }
      }
    
      $args = array(
        'post_type'      => 'stellenangebote',
        'stellen'        => $page_slug,
        'orderby'        => 'title',
        'order'          => 'ASC',
        'posts_per_page' => -1,
      );
      $query = new WP_Query( $args );
      $custom_select = '';
    
      if ( $query->have_posts() ) {
        $custom_select .= sprintf( '<span class="wpcf7-form-control-wrap %s-wrap">', $tag->name );
        $custom_select .= sprintf( '<select id="%1$s" class="%1$s" name="%1$s" class="wpcf7-form-control wpcf7-select wpcf7-validates-as-required" id="%1$s" aria-required="true" aria-invalid="false">',  $tag->name );
        $custom_select .= sprintf( '<option value="" class="%1$s">Bitte Job w?hlen</option>', $tag->name );
    
        while ( $query->have_posts() ) {
          $query->the_post();
    
          $the_title = the_title( '', '', false );
          $custom_select .= sprintf( '<option value="%1$s" class="%2$s--selected" >%1$s</option>',  $the_title, $tag->name);
        }
        $custom_select .= '</select>';
        $custom_select .= '</span>';
      }
    
      // restore orginal query
      wp_reset_postdata();
    
      return $custom_select;
    }
    function custom_radio_confirmation_validation_filter( $result, $tag ) {
      if ( 'bewerber-angebote' == $tag->name ) {
        $test_custom_select = $_POST['bewerber-angebote'];
    
        if ( empty( $test_custom_select ) ) {
          $result->invalidate( $tag, 'Das Feld ist erforderlich.' );
        }
      }
    
      return $result;
    }
    
    add_filter( 'wpcf7_validate_angebote*', 'custom_radio_confirmation_validation_filter', 20, 2 );
    Plugin Author Takayuki Miyoshi

    (@takayukister)

    The custom validation is actually working, but the validation error isn’t output because of a wrong class attribute.

    Change this line:

    $custom_select .= sprintf( '<span class="wpcf7-form-control-wrap %s-wrap">', $tag->name );

    to:

    $custom_select .= sprintf( '<span class="wpcf7-form-control-wrap %s">', $tag->name );

    Thread Starter Olivier Raimbaud

    (@oraimbaud)

    Oh right!
    Domo arigato!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom validation cf7 5.0 not working anymore’ is closed to new replies.