--- unique_field.module.20100121	2010-04-14 13:54:49.000000000 +1000
+++ unique_field.module	2010-04-14 15:51:57.000000000 +1000
@@ -311,8 +311,12 @@ function unique_field_match_value($field
         if (!isset($db['columns'][$key]['column'])) {
           continue;
         }
-        // skip if the value is empty or is not a scalar
-        if (empty($val) || !is_scalar($val)) {
+        // skip values that are neither scalar or null (i.e. arrays, objects, etc.)
+        if (!is_scalar($val) && !is_null($val)) {
+          continue;
+        }
+        // unless we want to compare empty values skip if the value is empty
+        elseif (!variable_get('unique_field_compare_empty_'. $ntype, NULL) && empty($val)) {
           continue;
         }
         // if query is not empty, add AND operator
@@ -320,19 +324,24 @@ function unique_field_match_value($field
           $qwhere_val .= 'AND ';
         }
         // generate comparison statement depending on field type
-        $qwhere_val .= $qtbl .'.'. db_escape_string($db['columns'][$key]['column']) .' = ';
-        $dbtype = $db['columns'][$key]['type'];
-        if ($dbtype == 'char' || $dbtype == 'varchar' || $dbtype == 'tinytext' || $dbtype == 'text' || $dbtype == 'mediumtext' || $dbtype == 'longtext' || $dbtype == 'datetime' || $dbtype == 'date' || $dbtype == 'time' || $dbtype == 'timestamp') {
-          $qwhere_val .= "'". db_escape_string($val) ."' ";
-        }
-        elseif (is_numeric($val)) {
-          $qwhere_val .= db_escape_string($val) ." ";
+        if (is_null($val)) {
+          $qwhere_val .= $qtbl .'.'. db_escape_string($db['columns'][$key]['column']) .' IS NULL ';
         }
         else {
-          $msg = t('Could not formulate query for unique_field_match_value on @field with data type @dbtype.', array('@field' => $field, '@dbtype' => $dbtype));
-          drupal_set_message($msg, 'error');
-          watchdog('unique_field', $msg, array(), WATCHDOG_WARNING);
-          return;
+          $qwhere_val .= $qtbl .'.'. db_escape_string($db['columns'][$key]['column']) .' = ';
+          $dbtype = $db['columns'][$key]['type'];
+          if ($dbtype == 'char' || $dbtype == 'varchar' || $dbtype == 'tinytext' || $dbtype == 'text' || $dbtype == 'mediumtext' || $dbtype == 'longtext' || $dbtype == 'datetime' || $dbtype == 'date' || $dbtype == 'time' || $dbtype == 'timestamp') {
+            $qwhere_val .= "'". db_escape_string($val) ."' ";
+          }
+          elseif (is_numeric($val)) {
+            $qwhere_val .= db_escape_string($val) ." ";
+          }
+          else {
+            $msg = t('Could not formulate query for unique_field_match_value on @field with data type @dbtype.', array('@field' => $field, '@dbtype' => $dbtype));
+            drupal_set_message($msg, 'error');
+            watchdog('unique_field', $msg, array(), WATCHDOG_WARNING);
+            return;
+          }
         }
       }
       if (!empty($qwhere_val)) {
@@ -444,6 +453,12 @@ function unique_field_node_settings_form
     '#default_value' => variable_get('unique_field_comp_'. $ntype, UNIQUE_FIELD_COMP_EACH),
     '#description' => t('For example, if you have fields for the parts of a street address (street number and name, city, and zip code) on a node, and want to allow only one node per complete address, but not only one node per city or per zip code, then you would want to choose that the fields must be unique in combination.'),
   );
+  $form['unique_field']['unique_field_compare_empty'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Check this box to compare empty values when checking for uniqueness'),
+    '#options' => array(UNIQUE_FIELD_COMPARE_EMPTY => t('Enabled')),
+    '#default_value' => variable_get('unique_field_compare_empty_'. $ntype, array()),
+  );
   $form['unique_field']['unique_field_show_matches'] = array(
     '#type' => 'checkboxes',
     '#title' => t('Check this box to show which nodes match when duplicate values are found'),
