Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- # For Gravity Forms. Sometimes a form has a checkbox or radio button that is not always required - agree terms or confirm something.
- # In this case it is a radio button to confirm that the person submitting the form is insured before they sign up for a subscription.
- # Some supscriptions require that the user confirms insurance status, some do not.
- # These functions remove the field if it is not needed, validate it if it is.
- # ------------------------------------------------------------
- # Unset insurance field if it is not required
- # ------------------------------------------------------------
- add_filter("gform_pre_render_3", "sd_unset_insured");
- function sd_unset_insured($form){
- // the label of the insurance field
- $insuredfield = array('Insured');
- global $post;
- // get post meta to check if insurance is required
- $needsinsurance = get_post_meta($post->ID,'_sd_product_insurance',true);
- // If value is 0, not required so unset
- if ($needsinsurance==0) {
- foreach($form['fields'] as $key=>$field) {
- if(in_array($field['label'], $insuredfield)) {
- unset($form['fields'][$key]);
- }
- }
- }
- return $form;
- }
- # ------------------------------------------------------------
- # Validate insurance field if it is required
- # ------------------------------------------------------------
- add_filter('gform_validation_3', 'sd_require_insured');
- function sd_require_insured ($validation_result){
- // the relevant field by form id and field id
- $insured = array("3:9");
- $form = $validation_result["form"];
- global $post;
- // get post meta to check if insurance is required
- $needsinsurance = get_post_meta($post->ID,'_sd_product_insurance',true);
- // If value is 1, required so validate
- if($needsinsurance==1) {
- foreach($form['fields'] as &$field){
- // find out if the field exists
- if(in_array($form["id"] . ":" . $field['id'], $insured)){
- $insuredinput= RGForms::post("input_{$field["id"] }");
- // if no confirmation is indicated, return an error
- if(!isset ($insuredinput) || $insuredinput!=1) {
- $field['failed_validation'] = true;
- $field['validation_message'] = 'You must confirm that you are insured';
- $validation_result['is_valid'] = false;
- $validation_result['form'] = $form;
- }
- }
- }
- }
- return $validation_result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement