gravity-forms-custom-post-types icon indicating copy to clipboard operation
gravity-forms-custom-post-types copied to clipboard

Update form when CPT taxonomy terms change

Open jr00ck opened this issue 10 years ago • 0 comments
trafficstars

Currently, on both GF 1.8 & GF 1.9, if you populate a checkbox field with taxonomies it works fine at first. However, if you update your taxonomy terms by adding, editing, or deleting any terms, the form submission can get a validation error if it's a required field and you have only selected a term that has moved to an index position that didn't exist when the last form save occurred.

Steps to recreate:

  1. add a term
  2. go back to your front-end form and select the very last checkbox and try to submit

should get a validation error that the field is required even though you have one selected.

Steps to manually fix:

  1. Edit the form in question (this causes the field to load all terms again)
  2. Click update form (no need to change anything. The new terms were already reloaded on the page load and the update re-saves the new terms to the field).
  3. go back to your front-end form and select the very last checkbox and try to submit

Should go through fine now.

The plugin should handle this automatically. I have an idea on how to fix it using the GF API. Here's some pseudo code:

// hook into actions when terms change
add_action ( 'created_term', 'update_form_taxonomies' );
add_action ( 'edited_term', 'update_form_taxonomies' );
add_action ( 'delete_term', 'update_form_taxonomies' );

function update_form_taxonomies( $term_id, $taxonomy ){

// get current form ID (how do we get this??? maybe save an option in the DB anytime a form is saved with the "Populate with taxonomy" option checked?)

// retrieve the form object using the GF API
$form = GFAPI::get_form($form_id);

// get taxonomies

// loop checkboxes form field with taxonomies that need to be populated and set new checkbox choices accordingly

// update the form using the GF API
$success = GFAPI::update_form($form);

}

jr00ck avatar Mar 05 '15 00:03 jr00ck