Skip to content

Instantly share code, notes, and snippets.

@scottsweb
Last active March 11, 2024 10:22
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save scottsweb/913464 to your computer and use it in GitHub Desktop.
Save scottsweb/913464 to your computer and use it in GitHub Desktop.
Using a Gravity Forms filter to create a custom validation and check for existing UID in already saved entries.
<?php
/***************************************************************
* Function media_custom_validation
* Check the media form for duplicate inputs on the same day
***************************************************************/
add_filter('gform_validation', 'media_custom_validation');
function media_custom_validation($validation_result) {
global $form_email, $file_id;
// new gravity form
$rg = new RGFormsModel();
// grab the form
$form = $validation_result["form"];
// only validate the vault form
if ($form['title'] == "Vault") {
foreach($form['fields'] as &$field) {
// store email field in variable
if ($field['id'] == "2") {
$form_email = rgpost("input_{$field['id']}");
}
// store file ID in variable
if ($field['id'] == "3") {
$file_id = rgpost("input_{$field['id']}");
}
// check the uid field
if ($field['label'] == "UID") {
$value = rgpost("input_{$field['id']}");
$uid = md5($value.$form_email.$file_id);
// search gravity forms for this UID
$search = $rg->get_leads($form['id'], 0, 'DESC', $uid, 0, 1000);
// if UID already exists
if (count($search) > 0) {
// set the form validation to false
$validation_result["is_valid"] = false;
// specify the first field & second field to be invalid and provide custom validation message
$form["fields"][0]["failed_validation"] = true;
$form["fields"][0]["validation_message"] = __('You have already downloaded this file today.', 'sandb');
$form["fields"][1]["failed_validation"] = true;
$form["fields"][1]["validation_message"] = __('You have already downloaded this file today.', 'sandb');
// update the form in the validation result with the form object you modified
$validation_result["form"] = $form;
}
}
}
}
return $validation_result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment