Quantcast
Channel: Snippets Archives - Gravity Wiz
Viewing all articles
Browse latest Browse all 196

Require an Existing Value for Submission with Gravity Forms

$
0
0
View this code snippet on GitHub.

Let’s say a reference number is generated on Form A (using something like GP Unique ID) for the user’s order (represented by a Gravity Form entry). The reference number is emailed to the user.

On Form B, the user is able to contact you about their order by entering the reference number. If they fail to enter a valid reference number, a validation error is returned and they are unable to submit the form.

validate-that-a-value-exists-for-a-gravity-formThe form will generate this error unless a valid reference number is entered.
Looking for an simple way to generate a reference number for your Gravity Form submissions? GP Unique ID for Gravity Forms can help.
Check out this step-by-step tutorial on how to use GP Unique ID to generate a reference number for your Gravity Form submissions and use this snippet to validate those reference numbers on subsequent submissions.

How to Generate and Validate a Reference Number for Gravity Forms with GP Unique ID

Getting Started

  1. Check requirements
  2. Install the snippet
    • Copy and paste the entire snippet into your theme’s functions.php file.
  3. Configure the snippet

    • Once you have installed the snippet, look at the bottom of the snippet for this block of code:

      View this code snippet on GitHub.
    • The target form ID and field ID represent the form and field that should be validated and show the validation error when validation fails.
    • The source form ID and field ID represent the form and field that store the existing value that the value submitted in the target form and field should be checked against.
    • The validation message can be updated to any message you would like to indicate that the entered value did not match any existing values in the source form and field.

Parameters

  • target_form_id (integer) (required)

    This is the Form ID for the field that you will checking if a value already exists (Form B in the example above). There is no default value.

  • target_field_id (integer) (required)

    This is the Field ID for the field that you will checking if a value already exists (Form B in the example above). There is no default value.

  • source_form_id (integer) (required)

    This is the Form ID for the field that generated the reference number (Form A in the example above). There is no default value.

  • source_field_id (integer) (required)

    This is the Field ID for the field that generated the reference number (Form A in the example above). There is no default value.

  • validation_message (string) (optional)

    This is the error message that will be displayed if the value being tested does not already exist. Defaults to "Hey! This isn't a valid reference number."

  • field_map (array) (optional)

    An array of target and source field IDs; allows for creating multiple requirements that must match the same entry. Format should be:

    array(
        // TARGET_FIELD_ID1 => SOURCE_FIELD_ID1,
        2 => 3,
        5 => 11
    );
  • disable_ajax_validation (bool) (optional)

    Optionally disable AJAX validation. AJAX validation is enabled by default and will add a checkmark or an x-mark depending on whether the value exists.

Any questions?

This snippet is more of a building block rather than a final solution. Let us know how you’re using it and any questions you have about building on top of it to accomplish a more involved solution.

For an example of how this can be combined with other plugins to create a more robust solution, check out the How to Generate and Validate a Reference Number for Gravity Forms with GP Unique ID tutorial.

We hope you find this snippet useful! If you do, show us some love by sharing this article on the social media platform of your choice. Let’s keep the Gravity Wiz pumping out awesome snippets.


Viewing all articles
Browse latest Browse all 196