1

Topsitemaker hooked me up with this custom module that works great with one node when I enable it. My issue is I need it to work across multiple nid's. The field name and cid on all the nid's will stay the same, the only thing that will change will be the nid. I have been trying without success to get this to work. In this example it works for nid 4, but if the module is enabled, it will return the error for the same field on other nid's. Is there an easy way to get this module to work with several nid's?

'<?php

/**
 * 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 = 'application_name_director_name_manager_name';
  $cid = 10;
  $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('YOU HAVE ALREADY SUBMITTED FOR THAT APPLICATION, PLEASE SELECT A NEW APPLICATION.'));
  }
}

/**
 * Implements hook_form_alter().
 */
function sample_form_alter2(&$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';
  }
}
1

Would have been good if you explained what the module does so we don't have to read through the code to understand the module.

I am going to assume you mean the nid condition in the query.

The condition function of the query object defaults to 'IN' if you send it an array.
eg.

$nid = array(4, 5, 6);

and then:

->condition('nid', $nid)

will check nids 4, 5 and 6.
If you want to check all submitted webforms, just remove the nid condition.

|improve this answer|||||
  • That is exactly how I was trying to accomplish this, setting the $nid variable as an array. But I just figured it out, I can change the select option values on the different nid's, they were the same value which was causing the error. Thanks so much for your help though!!! – 200mg Apr 22 '15 at 18:56

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.