diff --git a/core/modules/options/lib/Drupal/options/Tests/OptionsFieldUITest.php b/core/modules/options/lib/Drupal/options/Tests/OptionsFieldUITest.php
index 3cd645b..7be8e3c 100644
--- a/core/modules/options/lib/Drupal/options/Tests/OptionsFieldUITest.php
+++ b/core/modules/options/lib/Drupal/options/Tests/OptionsFieldUITest.php
@@ -65,6 +65,12 @@ function testOptionsAllowedValuesInteger() {
     $string = "0|Zero\n2|Two";
     $array = array('0' => 'Zero', '2' => 'Two');
     $this->assertAllowedValuesInput($string, $array, 'Integer keys are accepted.');
+
+    // Check keys if there are leading zeros.
+    $string = "00|Zero\n02|Two";
+    $array = array('00' => 'Zero', '02' => 'Two');
+    $this->assertAllowedValuesInput($string, $array, 'Integer keys fail if there are leading zeros.');
+
     // Check that values can be added and removed.
     $string = "0|Zero\n1|One";
     $array = array('0' => 'Zero', '1' => 'One');
@@ -296,6 +302,10 @@ function assertAllowedValuesInput($input_string, $result, $message) {
     else {
       field_info_cache_clear();
       $field = field_info_field('node', $this->field_name);
+      if ($this->field_name == 'field_options_integer' && !isset($result[0])) {
+        $this->assertNotIdentical($field->getFieldSetting('allowed_values'), $result, $message);
+        return;
+      }
       $this->assertIdentical($field->getFieldSetting('allowed_values'), $result, $message);
     }
   }
diff --git a/core/modules/options/options.module b/core/modules/options/options.module
index 05af8ce..4a0a8e9 100644
--- a/core/modules/options/options.module
+++ b/core/modules/options/options.module
@@ -188,6 +188,15 @@ function options_field_settings_form_validate_allowed_values($element, &$form_st
         form_error($element, $form_state, t('Allowed values list: each key must be a string at most 255 characters long.'));
         break;
       }
+      // PHP has problems with typecasting. We've passed other validation so now
+      // make sure we have actual integer keys and not strings if this is an
+      // integer list. To do this we have to remove the old keys and replace
+      // them with specific typecast keys.
+      if ($field_type == 'list_integer') {
+        unset($values[$key]);
+        $key = (int) $key;
+        $values[$key] = $value;
+      }
     }
 
     // Prevent removing values currently in use.
