Al Waqar
Al Waqar

Reputation: 101

Custom validation message for select field in Contact form 7 WordPress

I want to show custom message for select field. I am using a plugin "Contact form 7 Custom validation" for custom message but it is not working for select field. Is there a hook that can modify my message only for select field, as rest of my validation message is fine.

Update:

I have the following fields:

<div class="form-half">
                              <label for="state" class="visuallyhidden">state</label>[select* state id:state first_as_label "State" "Alabama" "Alaska" "American Samoa" "Arizona" "Arkansas" "California" "Colorado" "Connecticut" "Delaware" "District of Columbia" "Florida" "Georgia" "Guam" "Hawaii" "Idaho" "Illinois" "Indiana" "Iowa" "Kansas" "Kentucky" "Louisiana" "Maine" "Maryland" "Massachusetts" "Michigan" "Minnesota" "Mississippi" "Missouri" "Montana" "Nebraska" "Nevada" "New Hampshire" "New Jersey" "New Mexico" "New York" "North Carolina" "North Dakota" "Northern Marianas Islands" "Ohio" "Oklahoma" "Oregon" "Pennsylvania" "Puerto Rico" "Rhode Island" "South Carolina" "South Dakota" "Tennessee" "Texas" "Utah" "Vermont" "Virginia" "Virgin Islands" "Washington" "West Virginia" "Wisconsin" "Wyoming"]</div>

And I have used the following hook to do that, but not working:

add_filter( 'wpcf7_validate_select*', 'custom_select_validation_filter', 20, 2 );

function custom_select_validation_filter( $result, $tag ) {
    if ( 'state' == $tag->name ) {

        echo $test_custom_select = $_POST['state'];
        if ( empty( $test_custom_select ) || $test_custom_select == 'State' ) {
            // Example of result
            $result->invalidate($tag, __( 'Please enter state name', 'CF7' ));
        }

    }

    return $result;
}

But that is not working.

Upvotes: 0

Views: 7672

Answers (2)

Ben Goossens
Ben Goossens

Reputation: 197

Try the following code:

add_filter( 'wpcf7_validate_select', 'custom_select_validation_filter', 20, 2 );


function custom_select_validation_filter( $result, $tag ) {
    if ( 'state' == $tag->name ) {

        $test_custom_select = $_POST['state'];
        if ( empty( $test_custom_select ) || $test_custom_select == 'State' ) {
            // Example of result
            $result->invalidate($tag, __( 'your-select is required', 'CF7' ));
        }

    }
    elseif ( 'second-select' == $tag->name ){

        $test_custom_select = $_POST['second-select'];
        if ( empty( $test_custom_select ) ) {
            // Example of result
            $result->invalidate($tag, __( 'second-select is required', 'CF7' ));
        }

    }

    return $result;
}

https://contactform7.com/2015/03/28/custom-validation/

Field CF7: [select state id:state first_as_label "State" "Alabama" "Alaska" "American Samoa" "Arizona" "Arkansas" "California" "Colorado" "Connecticut" "Delaware" "District of Columbia" "Florida" "Georgia" "Guam" "Hawaii" "Idaho" "Illinois" "Indiana" "Iowa" "Kansas" "Kentucky" "Louisiana" "Maine" "Maryland" "Massachusetts" "Michigan" "Minnesota" "Mississippi" "Missouri" "Montana" "Nebraska" "Nevada" "New Hampshire" "New Jersey" "New Mexico" "New York" "North Carolina" "North Dakota" "Northern Marianas Islands" "Ohio" "Oklahoma" "Oregon" "Pennsylvania" "Puerto Rico" "Rhode Island" "South Carolina" "South Dakota" "Tennessee" "Texas" "Utah" "Vermont" "Virginia" "Virgin Islands" "Washington" "West Virginia" "Wisconsin" "Wyoming"]

Upvotes: 1

user11283889
user11283889

Reputation:

tested and works.

// For the custom Price for shuttle transport
/**
 * Generates a HTML string of two or more `option` elements/tags.
 *
 * @see wpcf7_select_shuttleprice_form_tag_handler()
 *
 * @return string $html
 */
function shuttleprice() {

    $id_a = null;      
    $max_personen = get_field("maximale_anzahl", $id_a);
    $max_personen_gesamt = get_field("anzahl_maximale_personen_im_shuttle_mit_aufpreis", $id_a);
    $aufpreis = get_field("aufpreis_pro_person_im_shuttle", $id_a);

    $inkl = "";
    $more = "";

    for ($x = 1; $x <= $max_personen; $x++) {
        if($x == 1) {
            $inkl = $inkl."<option value='".$x."'>für ".$x." Person (inklusive)</option>";
        } else {
            $inkl = $inkl."<option value='".$x."'>für ".$x." Personen (inklusive)</option>";
        }
    }

    if($max_personen_gesamt != "") {
        $lauf = 1;
        for ($x = $max_personen + 1; $x <= $max_personen_gesamt; $x++) {
            $more = $more.'<option data-price="'.$aufpreis*$lauf.'" value="'.$x.'">für '.$x.' Personen ('.$aufpreis*$lauf.' € Aufpreis)</option>';
            $lauf++;
        }
    }

    $html = '<option value="0">bitte wählen</option>'.$inkl.$more;

    return $html;
}


add_action( 'wpcf7_init', 'wpcf7_add_form_tag_select_shuttleprice' );
function wpcf7_add_form_tag_select_shuttleprice() {
    wpcf7_add_form_tag(
        array(
            'select_shuttleprice',
            'select_shuttleprice*',
        ),
        'wpcf7_select_shuttleprice_form_tag_handler',
        array(
            'name-attr'         => true,
            'selectable-values' => true,
        )
    );
}

function wpcf7_select_shuttleprice_form_tag_handler( $tag ) {
    return str_replace( '</select>', shuttleprice() . '</select>', str_replace(
        '<option value="">---</option>', '', wpcf7_select_form_tag_handler( $tag )
    ) );
}


add_filter( 'wpcf7_validate_select_shuttleprice', 'wpcf7_select_shuttleprice_validation_filter', 10, 2 );
add_filter( 'wpcf7_validate_select_shuttleprice*', 'wpcf7_select_shuttleprice_validation_filter', 10, 2 );
function wpcf7_select_shuttleprice_validation_filter( $result, $tag ) {
    $name = $tag->name;
    $empty = ( empty( $_POST[ $name ] ) || '0' === $_POST[ $name ] );

    if ( $tag->is_required() && $empty ) {
        $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );
    }

    return $result;
}

With this shortcode

[select_shuttleprice* shuttleprice-1 class:shuttleprice]

Upvotes: 0

Related Questions