Forked from scottsweb/gravity-forms-validation.php
Last active
August 29, 2015 14:17
-
-
Save jevgen/2563f8426c079d7a8266 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*************************************************************** | |
* 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