Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@ThemeCatcher ThemeCatcher commented Jun 23, 2020

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
You can’t perform that action at this time.