How to Conditionally Re-Use the Same Form [RESOLVED]

Greetings,

I have a series of sequential forms that will be filled out once if Applicant is single (Applicant 01), and twice if Applicant represents a couple (Applicant 02). Only Applicant 01 is an actual User in the system. We simply need information on Applicant 02, so no need for an additional registration.

Currently, I’m adding duplicating the forms for Applicant 02. But duplicate forms feels inefficient.

  1. Applicant 01: Contact information
  2. Applicant 01: Personal Info (Religion, DOB, Height, Weight, Phys. Description, etc.)
  3. Applicant 01: Employment Information (Employer, Job Title, Salary, etc.)
  4. Applicant 01: Health Information (Current Health, Allergies, Hospitalizations, etc.)
  5. Applicant 01: Legal (Arrests, Convictions, Charges, etc.)
  6. Applicant 02: Contact information
  7. Applicant 02: Personal Info (Religion, DOB, Height, Weight, Phys. Description, etc.)
  8. Applicant 02: Employment Information (Employer, Job Title, Salary, etc.)
  9. Applicant 02: Health Information (Current Health, Allergies, Hospitalizations, etc.)
  10. Applicant 02: Legal (Arrests, Convictions, Charges, etc.)

I’d like to to use the same forms TWICE while (in some way) maintaining a relationship between the applicants such that I am conditionally using the same form based on an is_couple (Radio Button) choice.

  1. Contact information
  2. Personal Info (Religion, DOB, Height, Weight, Phys. Description, etc.)
  3. Employment Information (Employer, Job Title, Salary, etc.)
  4. Health Information (Current Health, Allergies, Hospitalizations, etc.)
  5. Legal (Arrests, Convictions, Charges, etc.)

Be advised: I’m sure I can create some sort of “Workflow” via GravityFlow, but I find that interface design …visually challenging. However I’m still pretty new to GravityFlow, so the visual challenges may be due to my lack of knowledge thus far.

I’m curious as to whether this can be done without GravityFlow, be it via native GravityForms, Gravity Perks, etc. or do I need to code up something custom.

Please advise,

Preston

Well,

Since I got no nibbles on a response, I decided to hack my own solution which tracks users and forms submitted, allows users to edit previously submitted forms, allows admin’s to add/delete forms which dynamically populate the form list table on right in proper sequence (see graphic).

I’m particularly happy with the form navigation which is more highly customized (for me) than using GravityFlow’s “Checklists” addon.

Still have not figured out whether it’s better to use the same form for multiple entries or duplication the form, so I went with duplicating the forms (esp. since i’m using Easy Pass Through to repopulate form fields).

1 Like

Hi Preston,
I have just come across your comments and am interested to know how you created the form list table ‘HSA - 03 - Personal Info’ using gravity forms. I have purchased Gravity Flow but am looking for a way to create a checklist type sequence, but structure the forms in sequence based on conditional logic.
Roger

Greetings Roger,

This was done with PHP and the GravityForms API. Namely GFAPI::get_forms() and GFAPI::count_entries().

First, I needed to get a list of all the forms:
$forms = GFAPI::get_forms();

Then I looped through all the forms (since I was only looking for certain forms, I added a hidden field inside the forms I wanted).

function get_target_forms(  $forms ) {
    foreach ( $forms as $form ) :

            // Get all fields for the current form. 
        	$fields = $form['fields'];

            // Loop through the fields... 
        	foreach ( $fields as $field ) :
                // find my hidden field.
                if ( 'my_hidden_field' === $field->label ) :

                  // Store the bits I need in an array (abbreviated).
                  $target_forms[] = array(
                     'form_id'          => $form['id'],
                     'form_name'        => $form['title'],
                  );

                endif;
            endforeach;
    endforeach;

  return $target_forms;
}

Now I call the function to get an array of forms

$target_forms = get_target_forms( $forms );

Now that I have an array of just my target forms, I need a function for checking entries for the current user.

function check_user_submissions( int $form_id ): bool {
	// Get the current user
	$current_user = wp_get_current_user();

	// Set is_submitted to false
	$is_submitted = false;

	// The required search criteria
	$search_criteria = array(
		'status'        => 'active',
		'field_filters' => array(
			array(
				'key'   => 'created_by',
				'value' => $current_user->ID,
			)
		)
	);

	// Use the Gravity form api function to count the entries using our custom search criteria
	$entry_count = GFAPI::count_entries( $form_id, $search_criteria );

	// If the entry count is >= 1, then an entry was found.
	if ( $entry_count >= "1" ) {
		$is_submitted = true;
	}

	// Return the results as true or false.
	return $is_submitted;
}

Now its just a matter of looping the through $target_forms entries and checking for entries…

// Example: some holder array (or whatever data structure you wish...)
$data = null;  

// Loop through target forms
foreach( $target_forms as $the_form )  : 
     $data['is_submitted'] = check_user_submissions( $the_form['form_id'] );
endforeach;

And now you have an $data array with true or false for submitted entries.

Those are the broad strokes.

Hope this helps!

Preston

1 Like

Hi Preston,

Thank you for your reply and the information. I’m still learning the basics of PHP, but I understand the process and will try to replicate what you created.

Regards,
Roger

2 Likes

Thank you for posting your solution @SleeperTech

1 Like

No Prob.

1 Like

In case you hadn’t already found it - there is a setting for sequential forms in Gravity Flow Checklists and the gravityflowchecklists_checklists filter would allow you to apply some advanced logic if the order needs to change based on user/existing entries/etc. Don’t hesitate to contact our support via y our account page if you need a little more of a snippet example.

Cheers,
Jamie

Hi Jamie,

I appreciate the follow-up and the snippet.

Regards,
Roger

1 Like