Dynamically adding additional fields
-
Hi,
I would like to dynamically add a bunch of fields to a form. I managed to get this working with a workaround but then the fields don’t validate. How can I validate some fields dynamically added to a form?
This is the code I used to dynamically add fields to the form:
wpcf7_add_shortcode('contactform_newcontact', 'contactform_add_newcontact', true); function contactform_add_newcontact() { $formhtml = preg_replace( "/\r|\n/", "", '<input type="text" name="textbox1[]" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false"> <br /> <input type="text" name="textbox2[]" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false"> <br /> <input type="text" name="textbox3[]" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false"> <br /> <input type="text" name="textbox4[]" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false">'); $output = $formhtml . '<div name="cf-btn-wrapper" id="cf-btn-wrapper"><input type="button" name="cf-addbtn[]" id="cf-addbtn[]" value="Add Another" class="wpcf7-form-control wpcf7-submit avia-button avia-size-large cf-addcontact"></div>'; $output .= '<script type="text/javascript"> jQuery(document).ready(function() { jQuery(".cf-addcontact").live("click",function() { jQuery("#cf-btn-wrapper").before(\'' . $formhtml . '\'); jQuery("form.wpcf7-form").validate(); // This doesn't work }); }); </script>'; return $output; }
I found this article but it’s for a specific field that pre-exists on the form: https://contactform7.com/2015/03/28/custom-validation/
How can I invalidate a dynamically added textbox? i.e. calling something like jQuery(“form.wpcf7-form”).validate(); after the fields have been added to re-validate all fields? Or hook into validation server side?
- The topic ‘Dynamically adding additional fields’ is closed to new replies.