function FileItem::validateMaxFilesize

Same name and namespace in other branches
  1. 8.9.x core/modules/file/src/Plugin/Field/FieldType/FileItem.php \Drupal\file\Plugin\Field\FieldType\FileItem::validateMaxFilesize()
  2. 10 core/modules/file/src/Plugin/Field/FieldType/FileItem.php \Drupal\file\Plugin\Field\FieldType\FileItem::validateMaxFilesize()
  3. 11.x core/modules/file/src/Plugin/Field/FieldType/FileItem.php \Drupal\file\Plugin\Field\FieldType\FileItem::validateMaxFilesize()

Form API callback.

Ensures that a size has been entered and that it can be parsed by \Drupal\Component\Utility\Bytes::toNumber().

This function is assigned as an #element_validate callback in fieldSettingsForm().

File

core/modules/file/src/Plugin/Field/FieldType/FileItem.php, line 261

Class

FileItem
Plugin implementation of the 'file' field type.

Namespace

Drupal\file\Plugin\Field\FieldType

Code

public static function validateMaxFilesize($element, FormStateInterface $form_state) {
    $element['#value'] = trim($element['#value']);
    $form_state->setValue([
        'settings',
        'max_filesize',
    ], $element['#value']);
    if (!empty($element['#value']) && !Bytes::validate($element['#value'])) {
        $form_state->setError($element, new TranslatableMarkup('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).', [
            '@name' => $element['#title'],
        ]));
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.