skip to Main Content

I am new to wordpress php and have a question regarding custom contact form 7 validation.

I have the code below and wish to add this to return the error msg if not selected.

function add_shortcode_metalspurityradio() {
    wpcf7_add_shortcode( 'goldkarat', 'metalspurityradio_handler', true );
}
add_action( 'wpcf7_init', 'add_shortcode_metalspurityradio' );

function metalspurityradio_handler( $tag ){
    $tag = new WPCF7_FormTag( $tag );

    $atts = array(
        'type' => 'radio',
        'name' => $tag->name,
        'list' => $tag->name . '-options' );

    $input = sprintf(
        '<input %s />',
        wpcf7_format_atts( $atts ) );
        $datalist = '';
        $datalist .= '<div class="goldkaratradio d-flex">';
        foreach ( $tag->values as $val ) {
        list($radiovalue,$imagepath) = explode("!", $val
    );

    $datalist .= sprintf(
     '<div class="quotetabswrap col-md-1 col-xs-2 p-1">
        <label class="quotetabslabel p-1 pb-0 mb-1">
            <input type="radio" name="%s" value="%s" class="hideradio" />
            <div class="quotetabsshape">
                <div id="'.$radiovalue.'" class="quotetabstext">%s</div>
            </div>
        </label>
     </div>', $tag->name, $radiovalue, $radiovalue, $radiovalue
    );

    }
    $datalist .= '</div>';

    return $datalist;
}

Just want it to be validated if required is selected
ie:
[goldkarat* metals-purity
"1K"
"2K"
"3K"
"4K"
"5K"
"6K"
"7K"
"8K"
"9K"
]

2

Answers


  1. Hello try the below code it should working with you

    function add_shortcode_metalspurityradio() {
        wpcf7_add_form_tag( 'goldkarat', 'metalspurityradio_handler', true );
    }
    add_action( 'wpcf7_init', 'add_shortcode_metalspurityradio' );
    
    function metalspurityradio_handler( $tag ) {
        $tag = new WPCF7_FormTag( $tag );
    
        $atts = array(
            'type' => 'radio',
            'name' => $tag->name,
        );
    
        $datalist = '';
        $datalist .= '<div class="goldkaratradio d-flex">';
        foreach ( $tag->values as $val ) {
            list($radiovalue, $imagepath) = explode("!", $val );
    
            $datalist .= sprintf(
                '<div class="quotetabswrap col-md-1 col-xs-2 p-1">
                    <label class="quotetabslabel p-1 pb-0 mb-1">
                        <input type="radio" name="%s" value="%s" class="hideradio" />
                        <div class="quotetabsshape">
                            <div id="%s" class="quotetabstext">%s</div>
                        </div>
                    </label>
                 </div>', 
                esc_attr($tag->name), esc_attr($radiovalue), esc_attr($radiovalue), esc_html($radiovalue)
            );
        }
        $datalist .= '</div>';
    
        return $datalist;
    }
    
    function custom_validate_goldkarat($result, $tag) {
        $tag = new WPCF7_FormTag( $tag );
    
        if ($tag->is_required() && empty($_POST[$tag->name])) {
            $result->invalidate( $tag, "Please select a gold karat option." );
        }
    
        return $result;
    }
    add_filter('wpcf7_validate_goldkarat', 'custom_validate_goldkarat', 10, 2);
    add_filter('wpcf7_validate_goldkarat*', 'custom_validate_goldkarat', 10, 2);
    
    Login or Signup to reply.
  2. the answer of Sayed Mohamed was almost good, I corrected it below because I cannot edit it. then mark his answer as good.

    function add_shortcode_metalspurityradio() {
        wpcf7_add_form_tag( 'goldkarat', 'metalspurityradio_handler', true );
        wpcf7_add_form_tag( 'goldkarat*', 'metalspurityradio_handler', true );
    }
    add_action( 'wpcf7_init', 'add_shortcode_metalspurityradio' );
    
    function metalspurityradio_handler( $tag )
    {
        $tag = new WPCF7_FormTag( $tag );
    
        $atts = array(
            'type' => 'radio',
            'name' => $tag->name,
        );
    
        $datalist  = '';
        $datalist .= "<span class="wpcf7-form-control-wrap" data-name="{$tag->name}">";
        
        foreach ( $tag->values as $val ) {
            
            $datalist .= sprintf(
                '<span class="quotetabswrap col-md-1 col-xs-2 p-1">
                    <label class="quotetabslabel p-1 pb-0 mb-1">
                        <input type="radio" name="%s" value="%s" class="hideradio"/>
                        <span class="quotetabsshape">
                            <span id="%s" class="quotetabstext">%s</span>
                        </span>
                    </label>
                 </span>', 
                esc_attr($tag->name), esc_attr($val), esc_attr($val), esc_html($val)
            );
        }
        $datalist .= '</span>';
        
        $datalist .= wpcf7_get_validation_error( $tag->name );
        
    
        return $datalist;
    }
    
    function custom_validate_goldkarat($result, $tag)
    {
        $tag = new WPCF7_FormTag( $tag );
    
        if ($tag->is_required() && empty($_POST[$tag->name])) {
            $result->invalidate( $tag, "Please select a gold karat option." );
        }
    
        return $result;
    }
    add_filter('wpcf7_validate_goldkarat', 'custom_validate_goldkarat', 10, 2);
    add_filter('wpcf7_validate_goldkarat*', 'custom_validate_goldkarat', 10, 2);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search