If I understood your question correctly, Webform supports this out of the box.
Edit the "app name" field and under "Validation" select the "Unique" checkbox:
Check that all entered values for this field are unique. The same value is not allowed to be used twice.
This will make sure that each webform entry has a unique value for this field, meaning that duplicates will not be allowed.
UPDATE
Here is a sample code that will allow you to validate the webform submissions.
Create a new folder called custom
in sites/all/modules
, and then another folder called sample
in custom
.
Place the following in sample.info
file:
name = Sample
description = Sample custom module that provides custom webform validation rules.
package = Custom
core = 7.x
version = 7.x-1.0
Place the following in sample.module
file:
/**
* Implements hook_form_alter().
*/
function sample_form_alter(&$form, &$form_state, $form_id) {
// Make sure that we are selecting the webform forms.
if (substr($form_id, 0, 20) == 'webform_client_form_') {
$form['#validate'][] = 'sample_webform_validate';
}
}
/**
* Sample validation callback.
*/
function sample_webform_validate($form, &$form_state) {
// Replace this with the name of your field, cid (webform component ID) and
// nid.
$field_name = 'select_something';
$cid = 1;
$nid = 4;
// Check if this is a duplicate.
$is_duplicate = db_select('webform_submitted_data', 'd')
->fields('d')
->condition('nid', $nid)
->condition('cid', $cid)
->condition('data', $form_state['values']['submitted'][$field_name])
->execute()
->fetchObject();
if ($is_duplicate) {
form_set_error($field_name, t('Duplicate value, please select something else.'));
}
}