From cf694b1e27f48bc77cfabf70f2a7997335a5f027 Mon Sep 17 00:00:00 2001
From: Bob Vincent <bobvin@pillars.net>
Date: Fri, 10 Jun 2011 16:47:24 -0400
Subject: [PATCH] Issue #1008402 by LoMo: Improve validation of float and decimal field values.

---
 modules/field/modules/number/number.module |   16 ++++++++++++++--
 modules/field/modules/number/number.test   |   22 ++++++++++++++++++++++
 2 files changed, 36 insertions(+), 2 deletions(-)

diff --git a/modules/field/modules/number/number.module b/modules/field/modules/number/number.module
index 20e380777f649213f80fcad35dd35c5135f2ef9f..3c8132cb31e404d9fa4432be3cc6675bd007fbbd 100644
--- a/modules/field/modules/number/number.module
+++ b/modules/field/modules/number/number.module
@@ -379,9 +379,21 @@ function number_field_widget_validate($element, &$form_state) {
       form_error($element, $message);
     }
     else {
-      // Substitute the decimal separator,
       if ($type == 'decimal' || $type == 'float') {
-        $value = strtr($value, $field['settings']['decimal_separator'], '.');
+        // Verify that only one decimal separator exists in the field.
+        if (substr_count($value, $field['settings']['decimal_separator']) > 1) {
+          $message = t('%field: There should only be one decimal separator (@separator).',
+            array(
+              '%field' => t($instance['label']),
+              '@separator' => $field['settings']['decimal_separator'],
+            )
+          );
+          form_error($element, $message);
+        }
+        else {
+          // Substitute the decimal separator; things should be fine.
+          $value = strtr($value, $field['settings']['decimal_separator'], '.');
+        }
       }
       form_set_value($element, $value, $form_state);
     }
diff --git a/modules/field/modules/number/number.test b/modules/field/modules/number/number.test
index ec100f189796e9179e0167251fbed4c122387d45..3b0cbafae3247b87e3e666005b3518a6f28ce2a3 100644
--- a/modules/field/modules/number/number.test
+++ b/modules/field/modules/number/number.test
@@ -70,5 +70,27 @@ class NumberFieldTestCase extends DrupalWebTestCase {
     $id = $match[1];
     $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), t('Entity was created'));
     $this->assertRaw(round($value, 2), t('Value is displayed.'));
+
+    // Try to create entries with more than one decimal separator; assert fail.
+    $wrong_entries = array(
+      '3.14.159',
+      '0..45469',
+      '..4589',
+      '6.459.52',
+      '6.3..25',
+    );
+
+    foreach ($wrong_entries as $wrong_entry) {
+      $this->drupalGet('test-entity/add/test-bundle');
+      $edit = array(
+        "{$this->field['field_name']}[$langcode][0][value]" => $wrong_entry,
+      );
+      $this->drupalPost(NULL, $edit, t('Save'));
+      $this->assertText(
+        t('There should only be one decimal separator (@separator)',
+          array('@separator' => $this->field['settings']['decimal_separator'])),
+        t('Correctly failed to save decimal value with more than one decimal point.')
+      );
+    }
   }
 }
-- 
1.7.4.1

