You are here

function webform_validate_unique in Webform 7.3

Same name and namespace in other branches
  1. 6.3 includes/webform.components.inc \webform_validate_unique()
  2. 7.4 includes/webform.components.inc \webform_validate_unique()

Validate an element value is unique with no duplicates in the database.

3 string references to 'webform_validate_unique'
_webform_render_email in components/email.inc
Implements _webform_render_component().
_webform_render_number in components/number.inc
Implements _webform_render_component().
_webform_render_textfield in components/textfield.inc
Implements _webform_render_component().

File

includes/webform.components.inc, line 1104
Webform module component handling.

Code

function webform_validate_unique($element, $form_state) {
  if ($element['#value'] !== '') {
    $nid = $form_state['values']['details']['nid'];
    $sid = $form_state['values']['details']['sid'];
    $query = db_select('webform_submitted_data')
      ->fields('webform_submitted_data', array(
      'sid',
    ))
      ->condition('nid', $nid)
      ->condition('cid', $element['#webform_component']['cid'])
      ->condition('data', $element['#value'])
      ->range(0, 1);
    if ($sid) {
      $query
        ->condition('sid', $sid, '<>');
    }
    $count = $query
      ->execute()
      ->fetchField();
    if ($count) {
      form_error($element, t('The value %value has already been submitted once for the %title field. You may have already submitted this form, or you need to use a different value.', array(
        '%value' => $element['#value'],
        '%title' => $element['#title'],
      )));
    }
  }
}