Validate coupons (saved from another formidable form)

By: Mummy Ninja | Asked: 10/18/2023
ForumsCategory: Code HelpValidate coupons (saved from another formidable form)
Mummy NinjaMummy Ninja asked 9 months ago

Hi,

I found this code below on https://formidableforms.com/knowledgebase/frm_validate_field_entry/#kb-check-for-an-allowed-value-coupons

Looks like exactly what I need, but I need the posted value in the array to by dynamic rather than hard coded.

How can check another form (for example form B) if an entry key exists, then the coupon entered on form A is valid.

add_filter('frm_validate_field_entry', 'my_custom_validation', 10, 3);
function my_custom_validation($errors, $posted_field, $posted_value){
  if($posted_field->id == 25){ //change 25 to the ID of the field to validate
    if(!in_array($posted_value, array('001','002'))){ //change 001 and 002 to your allowed values
      //if it doesn't match up, add an error:
    $errors['field'. $posted_field->id] = 'That field is wrong!';
    }
  }
  return $errors;
}

OpenAI suggested to use global $wpdb object. Then do something like this:

// replace 'wp_frm_item_metas' with your table name and 'value' with your column name
$allowed_values = $wpdb->get_col('SELECT value FROM wp_frm_item_metas WHERE field_id = 11'); 

But maybe there's a better way within formidable forms alone. Like the API actions, etc.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right