diff --git a/core/modules/file/src/Plugin/Field/FieldType/FileItem.php b/core/modules/file/src/Plugin/Field/FieldType/FileItem.php index ba50903..0b59356 100644 --- a/core/modules/file/src/Plugin/Field/FieldType/FileItem.php +++ b/core/modules/file/src/Plugin/Field/FieldType/FileItem.php @@ -249,8 +249,9 @@ public static function validateExtensions($element, FormStateInterface $form_sta * fieldSettingsForm(). */ public static function validateMaxFilesize($element, FormStateInterface $form_state) { - if (!empty($element['#value']) && !is_numeric(Bytes::toInt($element['#value']))) { - $form_state->setError($element, t('The "@name" option must contain a valid value. You may either leave the text field empty or enter a string like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes).', array('@name' => $element['title']))); + $max_size = Bytes::toInt($element['#value']); + if (!empty($element['#value']) && $max_size == 0) { + $form_state->setError($element, t('The "@name" option must contain a valid value. You may either leave the text field empty or enter a string like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes).', array('@name' => $element['#title']))); } } diff --git a/core/modules/file/src/Tests/FileFieldDisplayTest.php b/core/modules/file/src/Tests/FileFieldDisplayTest.php index aa4923c..19716ba 100644 --- a/core/modules/file/src/Tests/FileFieldDisplayTest.php +++ b/core/modules/file/src/Tests/FileFieldDisplayTest.php @@ -170,4 +170,35 @@ function testDescToggle() { $this->assertText(t('The description may be used as the label of the link to the file.')); } + /** + * Test field settings form. + */ + function testFileFieldSettings() { + $field_name = strtolower($this->randomMachineName()); + $type_name = 'article'; + $field_storage_settings = [ + 'display_field' => '1', + 'display_default' => '0', + 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, + ]; + $field_settings = []; + $widget_settings = []; + $field = $this->createFileField($field_name, 'node', $type_name, $field_storage_settings, $field_settings, $widget_settings); + $edit[0]['settings[max_filesize]'] = '@@'; + $edit[1]['settings[max_filesize]'] = '0 MB'; + $edit[2]['settings[max_filesize]'] = '-1 KB'; + foreach($edit as $i){ + $this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.' . $field_name, $i, 'Save settings'); + $this->assertText('The "Maximum upload size" option must contain a valid value. You may either leave the text field empty or enter a string like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes).', 'Error message displayed for invalid max upload size'); + } + $edit_1[0]['settings[max_filesize]'] = '10'; + $edit_1[1]['settings[max_filesize]'] = '10 KB'; + $edit_1[2]['settings[max_filesize]'] = '10 MB'; + foreach($edit_1 as $i){ + $this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.' . $field_name, $i, 'Save settings'); + $this->assertText('Saved ' . $field_name . ' configuration.'); + $this->assertNoText('The "Maximum upload size" option must contain a valid value. You may either leave the text field empty or enter a string like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes).', 'Error message not displayed for valid max upload size'); + } + } + }