Skip to content

Instantly share code, notes, and snippets.

@ThemeCatcher
Created June 23, 2020 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThemeCatcher/d2934b3a4aa22d120ad1992ef1dff479 to your computer and use it in GitHub Desktop.
Save ThemeCatcher/d2934b3a4aa22d120ad1992ef1dff479 to your computer and use it in GitHub Desktop.
Validate that value exists in the entries for another form
add_filter('quform_element_valid_2_4', function ($valid, $value, Quform_Element_Field $element) {
if ($element->getForm()->config('environment') != 'frontend') {
return $valid;
}
global $wpdb;
$repository = quform('repository');
$emailElementId = '1_3';
list($formId, $elementId) = explode('_', $emailElementId);
$query = "SELECT `e`.`id` FROM `" . $repository->getEntryDataTableName() . "` ed LEFT JOIN `" .
$repository->getEntriesTableName() . "` e ON `ed`.`entry_id` = `e`.`id`
WHERE `e`.`form_id` = %d
AND `ed`.`element_id` = %d
AND `ed`.`value` = %s";
$args = array(
$formId,
$elementId,
$value
);
$result = $wpdb->get_row($wpdb->prepare($query, $args));
if ($result == null) {
$element->addError('Email address not found');
$valid = false;
}
return $valid;
}, 10, 3);
@ThemeCatcher
Copy link
Author

The code can be added to the WP theme functions.php file or using a plugin like My Custom Functions.

  • On line 1 replace 2_4 with the unique ID of the field you want to validate
  • On line 8 replace 1_3 with the unique ID of the field to search the entries for
  • On line 26 change the validation error message if necessary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment