Example #1
1
 /**
  * Form element validation handler for 'name' in form_test_validate_form().
  */
 public function validateName(&$element, FormStateInterface $form_state)
 {
     $triggered = FALSE;
     if ($form_state->getValue('name') == 'element_validate') {
         // Alter the form element.
         $element['#value'] = '#value changed by #element_validate';
         // Alter the submitted value in $form_state.
         $form_state->setValueForElement($element, 'value changed by setValueForElement() in #element_validate');
         $triggered = TRUE;
     }
     if ($form_state->getValue('name') == 'element_validate_access') {
         $form_state->set('form_test_name', $form_state->getValue('name'));
         // Alter the form element.
         $element['#access'] = FALSE;
         $triggered = TRUE;
     } elseif ($form_state->has('form_test_name')) {
         // To simplify this test, just take over the element's value into $form_state.
         $form_state->setValueForElement($element, $form_state->get('form_test_name'));
         $triggered = TRUE;
     }
     if ($triggered) {
         // Output the element's value from $form_state.
         drupal_set_message(t('@label value: @value', array('@label' => $element['#title'], '@value' => $form_state->getValue('name'))));
         // Trigger a form validation error to see our changes.
         $form_state->setErrorByName('');
     }
 }
 /**
  * Validate the color text field.
  */
 public function validate($element, FormStateInterface $form_state)
 {
     $value = $element['#value'];
     if (strlen($value) == 0) {
         $form_state->setValueForElement($element, '');
         return;
     }
     if (!preg_match('/^#([a-f0-9]{6})$/iD', strtolower($value))) {
         $form_state->setError($element, t("Color must be a 6-digit hexadecimal value, suitable for CSS."));
     }
 }
 /**
  * {@inheritdoc}
  */
 public function validateForm(array &$form, FormStateInterface $form_state)
 {
     if ($form_state->getValue('name') == 'validate') {
         // Alter the form element.
         $form['name']['#value'] = '#value changed by #validate';
         // Alter the submitted value in $form_state.
         $form_state->setValueForElement($form['name'], 'value changed by $form_state->setValueForElement() in #validate');
         // Output the element's value from $form_state.
         drupal_set_message(t('@label value: @value', array('@label' => $form['name']['#title'], '@value' => $form_state->getValue('name'))));
         // Trigger a form validation error to see our changes.
         $form_state->setErrorByName('');
     }
 }
Example #4
0
 /**
  * Form element validation handler for matched_path elements.
  *
  * Note that #maxlength is validated by _form_validate() already.
  *
  * This checks that the submitted value matches an active route.
  */
 public static function validateMatchedPath(&$element, FormStateInterface $form_state, &$complete_form)
 {
     if (!empty($element['#value']) && ($element['#validate_path'] || $element['#convert_path'] != self::CONVERT_NONE)) {
         /** @var \Drupal\Core\Url $url */
         if ($url = \Drupal::service('path.validator')->getUrlIfValid($element['#value'])) {
             if ($url->isExternal()) {
                 $form_state->setError($element, t('You cannot use an external URL, please enter a relative path.'));
                 return;
             }
             if ($element['#convert_path'] == self::CONVERT_NONE) {
                 // Url is valid, no conversion required.
                 return;
             }
             // We do the value conversion here whilst the Url object is in scope
             // after validation has occurred.
             if ($element['#convert_path'] == self::CONVERT_ROUTE) {
                 $form_state->setValueForElement($element, array('route_name' => $url->getRouteName(), 'route_parameters' => $url->getRouteParameters()));
                 return;
             } elseif ($element['#convert_path'] == self::CONVERT_URL) {
                 $form_state->setValueForElement($element, $url);
                 return;
             }
         }
         $form_state->setError($element, t('This path does not exist or you do not have permission to link to %path.', array('%path' => $element['#value'])));
     }
 }
 /**
  * {@inheritdoc}
  */
 public function defaultValuesFormSubmit(array $element, array &$form, FormStateInterface $form_state)
 {
     if ($form_state->getValue(['default_value_input', 'default_date_type']) || $form_state->getValue(['default_value_input', 'default_end_date_type'])) {
         if ($form_state->getValue(['default_value_input', 'default_date_type']) == static::DEFAULT_VALUE_NOW) {
             $form_state->setValueForElement($element['default_date'], static::DEFAULT_VALUE_NOW);
         }
         if ($form_state->getValue(['default_value_input', 'default_end_date_type']) == static::DEFAULT_VALUE_NOW) {
             $form_state->setValueForElement($element['default_end_date'], static::DEFAULT_VALUE_NOW);
         }
         return [$form_state->getValue('default_value_input')];
     }
     return [];
 }
Example #6
0
 /**
  * Implements \Drupal\Core\Form\FormInterface::validateForm().
  */
 public function validateForm(array &$form, FormStateInterface $form_state)
 {
     $routing_path = $form_state->getValue('routing_path');
     if ($routing_path) {
         $form_state->setValueForElement($form['routing']['routing_path'], $routing_path);
     } else {
         $form_state->setValueForElement($form['routing']['routing_path'], '/headless');
     }
     if ($routing_path[0] == '/') {
         $form_state->setErrorByName('routing_path', $this->t("The path '%path' cannot start with a slash.", array('%path' => $routing_path)));
     }
     if (!UrlHelper::isValid($routing_path)) {
         $form_state->setErrorByName('routing_path', $this->t("The path '%path' is invalid or you do not have access to it.", array('%path' => $routing_path)));
     }
 }
Example #7
0
 /**
  * Clear values from upload form element.
  *
  * @param array $element
  *   Upload form element.
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  *   Form state object.
  */
 protected function clearFormValues(array &$element, FormStateInterface $form_state)
 {
     // We propagated entities to the other parts of the system. We can now remove
     // them from our values.
     $form_state->setValueForElement($element['upload']['fids'], '');
     NestedArray::setValue($form_state->getUserInput(), $element['upload']['fids']['#parents'], '');
 }
Example #8
0
 /**
  * Form element validation handler for #type 'color'.
  */
 public static function validateColor(&$element, FormStateInterface $form_state, &$complete_form)
 {
     $value = trim($element['#value']);
     // Default to black if no value is given.
     // @see http://www.w3.org/TR/html5/number-state.html#color-state
     if ($value === '') {
         $form_state->setValueForElement($element, '#000000');
     } else {
         // Try to parse the value and normalize it.
         try {
             $form_state->setValueForElement($element, ColorUtility::rgbToHex(ColorUtility::hexToRgb($value)));
         } catch (\InvalidArgumentException $e) {
             $form_state->setError($element, t('%name must be a valid color.', array('%name' => empty($element['#title']) ? $element['#parents'][0] : $element['#title'])));
         }
     }
 }
 /**
  * @covers ::setValueForElement
  */
 public function testSetValueForElement()
 {
     $element = ['#type' => 'foo'];
     $value = 'BAR';
     $this->decoratedFormState->setValueForElement($element, $value)->shouldBeCalled();
     $this->assertSame($this->formStateDecoratorBase, $this->formStateDecoratorBase->setValueForElement($element, $value));
 }
Example #10
0
 /**
  * Form element validation handler for #type 'email'.
  *
  * Note that #maxlength and #required is validated by _form_validate() already.
  */
 public static function validateEmail(&$element, FormStateInterface $form_state, &$complete_form)
 {
     $value = trim($element['#value']);
     $form_state->setValueForElement($element, $value);
     if ($value !== '' && !valid_email_address($value)) {
         $form_state->setError($element, t('The email address %mail is not valid.', array('%mail' => $value)));
     }
 }
Example #11
0
 /**
  * Form element validation handler for #type 'url'.
  *
  * Note that #maxlength and #required is validated by _form_validate() already.
  */
 public static function validateUrl(&$element, FormStateInterface $form_state, &$complete_form)
 {
     $value = trim($element['#value']);
     $form_state->setValueForElement($element, $value);
     if ($value !== '' && !UrlHelper::isValid($value, TRUE)) {
         $form_state->setError($element, t('The URL %url is not valid.', array('%url' => $value)));
     }
 }
 /**
  * Element validation helper.
  */
 public static function multipleValidate($element, FormStateInterface $form_state)
 {
     $values = array_map('trim', explode(',', $element['#value']));
     $items = array();
     foreach ($values as $value) {
         $items[] = array('value' => $value);
     }
     $form_state->setValueForElement($element, $items);
 }
Example #13
0
 /**
  * Validates a password_confirm element.
  */
 public static function validatePasswordConfirm(&$element, FormStateInterface $form_state, &$complete_form)
 {
     $pass1 = trim($element['pass1']['#value']);
     $pass2 = trim($element['pass2']['#value']);
     if (!empty($pass1) || !empty($pass2)) {
         if (strcmp($pass1, $pass2)) {
             $form_state->setError($element, t('The specified passwords do not match.'));
         }
     } elseif ($element['#required'] && $form_state->getUserInput()) {
         $form_state->setError($element, t('Password field is required.'));
     }
     // Password field must be converted from a two-element array into a single
     // string regardless of validation results.
     $form_state->setValueForElement($element['pass1'], NULL);
     $form_state->setValueForElement($element['pass2'], NULL);
     $form_state->setValueForElement($element, $pass1);
     return $element;
 }
Example #14
0
 /**
  * Validation handler for the selection element.
  */
 public function validateSelectorUri($element, FormStateInterface $form_state, $form)
 {
     if (!empty($element['#value'])) {
         if (file_exists($file_path = $this->configuration['path'] . '/' . $element['#value'])) {
             $form_state->setValueForElement($element, $file_path);
         } else {
             $form_state->setErrorByName(implode('][', $element['#parents']), $this->t('The selected file does not exist.'));
         }
     }
 }
Example #15
0
 /**
  * {@inheritdoc}
  */
 public function validatePath(&$element, FormStateInterface $form_state)
 {
     // Ensure the path has a leading slash.
     $value = '/' . trim($element['#value'], '/');
     $form_state->setValueForElement($element, $value);
     // Ensure each path is unique.
     $path = $this->entityQuery->get('page')->condition('path', $value)->condition('id', $form_state->getValue('id'), '<>')->execute();
     if ($path) {
         $form_state->setErrorByName('path', $this->t('The page path must be unique.'));
     }
 }
 /**
  * Form element validate handler for language autocomplete element.
  */
 public static function validateElement($element, FormStateInterface $form_state)
 {
     if ($value = $element['#value']) {
         $languages = $element['#languagefield_options'];
         $langcode = array_search($value, $languages);
         if (!empty($langcode)) {
             $form_state->setValueForElement($element, $langcode);
         } else {
             $form_state->setError($element, t('An unexpected language is entered.'));
         }
     }
 }
Example #17
0
 /**
  * Validate video URL.
  */
 public function validateInput(&$element, FormStateInterface &$form_state, $form)
 {
     $input = $element['#value'];
     $video_id = youtube_get_video_id($input);
     if ($video_id && strlen($video_id) <= 20) {
         $video_id_element = array('#parents' => $element['#parents']);
         array_pop($video_id_element['#parents']);
         $video_id_element['#parents'][] = 'video_id';
         $form_state->setValueForElement($video_id_element, $video_id);
     } elseif (!empty($input)) {
         $form_state->setError($element, t('Please provide a valid YouTube URL.'));
     }
 }
Example #18
0
 /**
  * Form element validation handler for URL alias form element.
  *
  * @param array $element
  *   The form element.
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  *   The form state.
  */
 public static function validateFormElement(array &$element, FormStateInterface $form_state)
 {
     // Trim the submitted value of whitespace and slashes.
     $alias = trim(trim($element['alias']['#value']), " \\/");
     if (!empty($alias)) {
         $form_state->setValueForElement($element['alias'], $alias);
         // Validate that the submitted alias does not exist yet.
         $is_exists = \Drupal::service('path.alias_storage')->aliasExists($alias, $element['langcode']['#value'], $element['source']['#value']);
         if ($is_exists) {
             $form_state->setError($element, t('The alias is already in use.'));
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function validateForm(array &$form, FormStateInterface $form_state)
 {
     if ($form_state->getValue('name') == 'validate') {
         // Alter the form element.
         $form['name']['#value'] = '#value changed by #validate';
         // Alter the submitted value in $form_state.
         $form_state->setValueForElement($form['name'], 'value changed by setValueForElement() in #validate');
         // Output the element's value from $form_state.
         drupal_set_message(t('@label value: @value', array('@label' => $form['name']['#title'], '@value' => $form_state->getValue('name'))));
         // Trigger a form validation error to see our changes.
         $form_state->setErrorByName('');
         // To simplify this test, enable form caching and use form storage to
         // remember our alteration.
         $form_state->setCached();
     }
 }
 /**
  * Validate the fields and convert them into a single value as text.
  */
 public function validate($element, FormStateInterface $form_state)
 {
     // Validate each of the textfield entries.
     $values = array();
     foreach (array('r', 'g', 'b') as $colorfield) {
         $values[$colorfield] = $element[$colorfield]['#value'];
         // If they left any empty, we'll set the value empty and quit.
         if (strlen($values[$colorfield]) == 0) {
             $form_state->setValueForElement($element, '');
             return;
         }
         // If they gave us anything that's not hex, reject it.
         if (strlen($values[$colorfield]) != 2 || !ctype_xdigit($values[$colorfield])) {
             $form_state->setError($element[$colorfield], $form_state, t("Saturation value must be a 2-digit hexadecimal value between 00 and ff."));
         }
     }
     // Set the value of the entire form element.
     $value = strtolower(sprintf('#%02s%02s%02s', $values['r'], $values['g'], $values['b']));
     $form_state->setValueForElement($element, $value);
 }
Example #21
0
 /**
  * Form validation handler for widget elements.
  *
  * @param array $element
  *   The form element.
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  *   The form state.
  */
 public static function validateElement(array $element, FormStateInterface $form_state)
 {
     if ($element['#required'] && $element['#value'] == '_none') {
         $form_state->setError($element, t('@name field is required.', array('@name' => $element['#title'])));
     }
     // Massage submitted form values.
     // Drupal\Core\Field\WidgetBase::submit() expects values as
     // an array of values keyed by delta first, then by column, while our
     // widgets return the opposite.
     if (is_array($element['#value'])) {
         $values = array_values($element['#value']);
     } else {
         $values = array($element['#value']);
     }
     // Filter out the 'none' option. Use a strict comparison, because
     // 0 == 'any string'.
     $index = array_search('_none', $values, TRUE);
     if ($index !== FALSE) {
         unset($values[$index]);
     }
     // Transpose selections from field => delta to delta => field.
     $items = array();
     foreach ($values as $value) {
         $items[] = array($element['#key_column'] => $value);
     }
     $form_state->setValueForElement($element, $items);
 }
Example #22
0
 /**
  * Element validate; Check View is valid.
  */
 public static function settingsFormValidate($element, FormStateInterface $form_state, $form)
 {
     // Split view name and display name from the 'view_and_display' value.
     if (!empty($element['view_and_display']['#value'])) {
         list($view, $display) = explode(':', $element['view_and_display']['#value']);
     } else {
         $form_state->setError($element, t('The views entity selection mode requires a view.'));
         return;
     }
     // Explode the 'arguments' string into an actual array. Beware, explode()
     // turns an empty string into an array with one empty string. We'll need an
     // empty array instead.
     $arguments_string = trim($element['arguments']['#value']);
     if ($arguments_string === '') {
         $arguments = array();
     } else {
         // array_map() is called to trim whitespaces from the arguments.
         $arguments = array_map('trim', explode(',', $arguments_string));
     }
     $value = array('view_name' => $view, 'display_name' => $display, 'arguments' => $arguments);
     $form_state->setValueForElement($element, $value);
 }
Example #23
0
 /**
  * Form element validation handler for the 'uri' element.
  *
  * Disallows saving inaccessible or untrusted URLs.
  */
 public static function validateUriElement($element, FormStateInterface $form_state, $form)
 {
     $uri = static::getUserEnteredStringAsUri($element['#value']);
     $form_state->setValueForElement($element, $uri);
     // If getUserEnteredStringAsUri() mapped the entered value to a 'internal:'
     // URI , ensure the raw value begins with '/', '?' or '#'.
     // @todo '<front>' is valid input for BC reasons, may be removed by
     //   https://www.drupal.org/node/2421941
     if (parse_url($uri, PHP_URL_SCHEME) === 'internal' && !in_array($element['#value'][0], ['/', '?', '#'], TRUE) && substr($element['#value'], 0, 7) !== '<front>') {
         $form_state->setError($element, t('Manually entered paths should start with /, ? or #.'));
         return;
     }
 }
Example #24
0
 /**
  * Special submit handling.
  */
 public function submitOptionsForm(&$form, FormStateInterface $form_state) {
   $element = array('#parents' => array('query', 'options', 'query_tags'));
   $value = explode(',', NestedArray::getValue($form_state->getValues(), $element['#parents']));
   $value = array_filter(array_map('trim', $value));
   $form_state->setValueForElement($element, $value);
 }
Example #25
0
 /**
  * {@inheritdoc}
  */
 public function validateForm(array &$form, FormStateInterface $form_state)
 {
     // Check for empty front page path.
     if ($form_state->isValueEmpty('site_frontpage')) {
         // Set to default "user/login".
         $form_state->setValueForElement($form['front_page']['site_frontpage'], '/user/login');
     } else {
         // Get the normal path of the front page.
         $form_state->setValueForElement($form['front_page']['site_frontpage'], $this->aliasManager->getPathByAlias($form_state->getValue('site_frontpage')));
     }
     // Validate front page path.
     if (($value = $form_state->getValue('site_frontpage')) && $value[0] !== '/') {
         $form_state->setErrorByName('site_frontpage', $this->t("The path '%path' has to start with a slash.", ['%path' => $form_state->getValue('site_frontpage')]));
     }
     if (!$this->pathValidator->isValid($form_state->getValue('site_frontpage'))) {
         $form_state->setErrorByName('site_frontpage', $this->t("The path '%path' is either invalid or you do not have access to it.", array('%path' => $form_state->getValue('site_frontpage'))));
     }
     // Get the normal paths of both error pages.
     if (!$form_state->isValueEmpty('site_403')) {
         $form_state->setValueForElement($form['error_page']['site_403'], $this->aliasManager->getPathByAlias($form_state->getValue('site_403')));
     }
     if (!$form_state->isValueEmpty('site_404')) {
         $form_state->setValueForElement($form['error_page']['site_404'], $this->aliasManager->getPathByAlias($form_state->getValue('site_404')));
     }
     if (($value = $form_state->getValue('site_403')) && $value[0] !== '/') {
         $form_state->setErrorByName('site_403', $this->t("The path '%path' has to start with a slash.", ['%path' => $form_state->getValue('site_403')]));
     }
     if (($value = $form_state->getValue('site_404')) && $value[0] !== '/') {
         $form_state->setErrorByName('site_404', $this->t("The path '%path' has to start with a slash.", ['%path' => $form_state->getValue('site_404')]));
     }
     // Validate 403 error path.
     if (!$form_state->isValueEmpty('site_403') && !$this->pathValidator->isValid($form_state->getValue('site_403'))) {
         $form_state->setErrorByName('site_403', $this->t("The path '%path' is either invalid or you do not have access to it.", array('%path' => $form_state->getValue('site_403'))));
     }
     // Validate 404 error path.
     if (!$form_state->isValueEmpty('site_404') && !$this->pathValidator->isValid($form_state->getValue('site_404'))) {
         $form_state->setErrorByName('site_404', $this->t("The path '%path' is either invalid or you do not have access to it.", array('%path' => $form_state->getValue('site_404'))));
     }
     parent::validateForm($form, $form_state);
 }
Example #26
0
 /**
  * #element_validate callback for options field allowed values.
  *
  * @param $element
  *   An associative array containing the properties and children of the
  *   generic form element.
  * @param $form_state
  *   The current state of the form for the form this element belongs to.
  *
  * @see form_process_pattern()
  */
 public static function validateAllowedValues($element, FormStateInterface $form_state)
 {
     $values = static::extractAllowedValues($element['#value'], $element['#field_has_data']);
     if (!is_array($values)) {
         $form_state->setError($element, t('Allowed values list: invalid input.'));
     } else {
         // Check that keys are valid for the field type.
         foreach ($values as $key => $value) {
             if ($error = static::validateAllowedValue($key)) {
                 $form_state->setError($element, $error);
                 break;
             }
         }
         // Prevent removing values currently in use.
         if ($element['#field_has_data']) {
             $lost_keys = array_diff(array_keys($element['#allowed_values']), array_keys($values));
             if (_options_values_in_use($element['#entity_type'], $element['#field_name'], $lost_keys)) {
                 $form_state->setError($element, t('Allowed values list: some values are being removed while currently in use.'));
             }
         }
         $form_state->setValueForElement($element, $values);
     }
 }
 /**
  * Form element validation handler; Filters the #value property of an element.
  */
 public static function elementValidateFilter(&$element, FormStateInterface $form_state)
 {
     $element['#value'] = array_filter($element['#value']);
     $form_state->setValueForElement($element, $element['#value']);
 }
 /**
  * {@inheritdoc}
  */
 public function validateForm(array &$form, FormStateInterface $form_state)
 {
     // The confirmation step needs no additional validation.
     if ($this->data) {
         return;
     }
     // Decode the submitted import.
     $data = Yaml::decode($form_state->getValue('import'));
     // Validate for config entities.
     if ($form_state->getValue('config_type') !== 'system.simple') {
         $definition = $this->entityManager->getDefinition($form_state->getValue('config_type'));
         $id_key = $definition->getKey('id');
         // If a custom entity ID is specified, override the value in the
         // configuration data being imported.
         if (!$form_state->isValueEmpty('custom_entity_id')) {
             $data[$id_key] = $form_state->getValue('custom_entity_id');
         }
         $entity_storage = $this->entityManager->getStorage($form_state->getValue('config_type'));
         // If an entity ID was not specified, set an error.
         if (!isset($data[$id_key])) {
             $form_state->setErrorByName('import', $this->t('Missing ID key "@id_key" for this @entity_type import.', array('@id_key' => $id_key, '@entity_type' => $definition->getLabel())));
             return;
         }
         // If there is an existing entity, ensure matching ID and UUID.
         if ($entity = $entity_storage->load($data[$id_key])) {
             $this->configExists = $entity;
             if (!isset($data['uuid'])) {
                 $form_state->setErrorByName('import', $this->t('An entity with this machine name already exists but the import did not specify a UUID.'));
                 return;
             }
             if ($data['uuid'] !== $entity->uuid()) {
                 $form_state->setErrorByName('import', $this->t('An entity with this machine name already exists but the UUID does not match.'));
                 return;
             }
         } elseif (isset($data['uuid']) && $entity_storage->loadByProperties(array('uuid' => $data['uuid']))) {
             $form_state->setErrorByName('import', $this->t('An entity with this UUID already exists but the machine name does not match.'));
         }
     } else {
         $config = $this->config($form_state->getValue('config_name'));
         $this->configExists = !$config->isNew() ? $config : FALSE;
     }
     // Store the decoded version of the submitted import.
     $form_state->setValueForElement($form['import'], $data);
 }
Example #29
0
 /**
  * Validates the managed_file element for the default Image form.
  *
  * This function ensures the fid is a scalar value and not an array. It is
  * assigned as a #element_validate callback in
  * \Drupal\image\Plugin\Field\FieldType\ImageItem::defaultImageForm().
  *
  * @param array $element
  *   The form element to process.
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  *   The form state.
  */
 public static function validateDefaultImageForm(array &$element, FormStateInterface $form_state)
 {
     // Consolidate the array value of this field to a single FID as #extended
     // for default image is not TRUE and this is a single value.
     if (isset($element['fids']['#value'][0])) {
         $value = $element['fids']['#value'][0];
         // Convert the file ID to a uuid.
         if ($file = \Drupal::entityManager()->getStorage('file')->load($value)) {
             $value = $file->uuid();
         }
     } else {
         $value = '';
     }
     $form_state->setValueForElement($element, $value);
 }
 /**
  * Form API callback.
  *
  * This function is assigned as an #element_validate callback in
  * fieldSettingsForm().
  *
  * This doubles as a convenience clean-up function and a validation routine.
  * Commas are allowed by the end-user, but ultimately the value will be stored
  * as a space-separated list for compatibility with file_validate_extensions().
  */
 public static function validateExtensions($element, FormStateInterface $form_state)
 {
     if (!empty($element['#value'])) {
         $extensions = preg_replace('/([, ]+\\.?)/', ' ', trim(strtolower($element['#value'])));
         $extensions = array_filter(explode(' ', $extensions));
         $extensions = implode(' ', array_unique($extensions));
         if (!preg_match('/^([a-z0-9]+([.][a-z0-9])* ?)+$/', $extensions)) {
             $form_state->setError($element, t('The list of allowed extensions is not valid, be sure to exclude leading dots and to separate extensions with a comma or space.'));
         } else {
             $form_state->setValueForElement($element, $extensions);
         }
     }
 }