0

Is there a way to get Drupal 7 / Webforms to block a submission for a duplicate answer in a previous submission. I am sending an application questionnaire to multiple app owners, the form is setup with a listbox containing the App name, then the rest of the form is generic for each app. I would like for the App owners to not be able to accidentally submit for the same app twice.

Many people have suggested the "Webform Validation" module, but as far as I can tell, it only does validation on the form itself. It cannot do validation for the form being submitted against previously submitted forms.

TIA

0

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.'));
  }
}
  • Ok, so I clicked on the Application Name Field, selected "Edit" and when I scroll down to "Validation" it only has one option titled "Required" which can be checked or unchecked. This is Drupal 7, not sure if that makes a difference. – 200mg Apr 17 '15 at 18:37
  • Is the "Application Name" field a textfield? You can see my screenshot with a note here. – Aram Boyajyan Apr 17 '15 at 18:41
  • No, it's a Type: Select Options that I have displayed as a listbox. I checked one of my textfield's validation options and it does have the unique options, I guess it's disabled on "select options " – 200mg Apr 17 '15 at 18:43
  • This is a pretty good thread on it, I guess it's a no-can-do via the UI right now drupal.org/node/1151516 – 200mg Apr 17 '15 at 18:46
  • Sorry, now I see that you mentioned that the "App Name" is a select box. Well, I still think you could do this using a custom module. I'll update my answer to show you the direction. – Aram Boyajyan Apr 17 '15 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.