Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress Contact Form 7 radio not required

I have a form with few fields along with a radio button. This radio button is an optional field. But I am not able to submit form if I do not select any option from the radio button.

<div class="input-field-outer">
    [text* your-name class:name placeholder "Name or business name*"]
</div>
<div class="input-field-outer">
    [email* email placeholder "Email*"]
</div>
<div class="input-field-outer">
    [text* contact-number id:telno class:hs-phone-number placeholder "Contact number*"]
    <span id="errmsg"></span>
</div>
<div class="input-field-outer">
    <div class="radio-outer">
        [radio customer_type "New Customer" "Old Customer"]
    </div>
</div>

It will work when I make any of them selected default:

[radio customer_type default:1 "New Customer" "Old Customer"]

Any ideas about using radio button with no items default.

like image 757
Rijo Avatar asked Aug 14 '17 09:08

Rijo


2 Answers

To remove validation on all radio fields add this to functions.php or wherever appropriate.

remove_filter('wpcf7_validate_radio', 'wpcf7_checkbox_validation_filter', 10);

If you want to maintain the required validation for some of the radios, change them to this:

[radio fieldName class:required "Yes" "No"]

Then add this code to functions.php:

remove_filter('wpcf7_validate_radio', 'wpcf7_checkbox_validation_filter', 10);

add_filter('wpcf7_validate_radio', function($result, $tag) {
  if (in_array('class:required', $tag->options)) {
    $result = wpcf7_checkbox_validation_filter($result, $tag);
  }
  return $result;
}, 10, 2);
like image 39
Jamie Chong Avatar answered Oct 19 '22 23:10

Jamie Chong


A workaround would be to use a checkbox and add the exclusive parameter. This allows you to submit the form without selecting a customer type.

[checkbox customer_type exclusive "New Customer" "Old Customer"]
like image 99
vicente Avatar answered Oct 20 '22 01:10

vicente