Commit 26d253c7 authored by catch's avatar catch
Browse files

Issue #2239299 by tim.plunkett: Form errors should only be set during validation.

parent c4b7a5b8
Loading
Loading
Loading
Loading
+19 −5
Changes for core/lib/Drupal/Core/Form/FormBuilder.php: 19 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -824,17 +824,14 @@ public function validateForm($form_id, &$form, &$form_state) {
        // Stop here and don't run any further validation handlers, because they
        // could invoke non-safe operations which opens the door for CSRF
        // vulnerabilities.
        $this->validatedForms[$form_id] = TRUE;
        $this->finalizeValidation($form_id, $form, $form_state);
        return;
      }
    }

    // Recursively validate each form element.
    $this->doValidateForm($form, $form_state, $form_id);
    // After validation, loop through and assign each element its errors.
    $this->setElementErrorsFromFormState($form, $form_state);
    // Mark this form as validated.
    $this->validatedForms[$form_id] = TRUE;
    $this->finalizeValidation($form_id, $form, $form_state);

    // If validation errors are limited then remove any non validated form values,
    // so that only values that passed validation are left for submit callbacks.
@@ -877,6 +874,23 @@ public function validateForm($form_id, &$form, &$form_state) {
    }
  }

  /**
   * Finalizes validation.
   *
   * @param string $form_id
   *   The unique string identifying the form.
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param array $form_state
   *   An associative array containing the current state of the form.
   */
  protected function finalizeValidation($form_id, &$form, &$form_state) {
    // After validation, loop through and assign each element its errors.
    $this->setElementErrorsFromFormState($form, $form_state);
    // Mark this form as validated.
    $this->validatedForms[$form_id] = TRUE;
  }

  /**
   * {@inheritdoc}
   */
+1 −1
Changes for core/modules/aggregator/lib/Drupal/aggregator/Form/OpmlFeedAdd.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -114,7 +114,7 @@ public function validateForm(array &$form, array &$form_state) {
   */
  public function submitForm(array &$form, array &$form_state) {
    $validators = array('file_validate_extensions' => array('opml xml'));
    if ($file = file_save_upload('upload', $form_state, $validators, FALSE, 0)) {
    if ($file = file_save_upload('upload', $validators, FALSE, 0)) {
      $data = file_get_contents($file->getFileUri());
    }
    else {
+1 −1
Changes for core/modules/config/lib/Drupal/config/Form/ConfigImportForm.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -100,7 +100,7 @@ public function submitForm(array &$form, array &$form_state) {
        $form_state['redirect_route']['route_name'] = 'config.sync';
      }
      catch (\Exception $e) {
        $this->setFormError('import_tarball', $form_state, $this->t('Could not extract the contents of the tar file. The error message is <em>@message</em>', array('@message' => $e->getMessage())));
        drupal_set_message($this->t('Could not extract the contents of the tar file. The error message is <em>@message</em>', array('@message' => $e->getMessage())), 'error');
      }
      drupal_unlink($path);
    }
+4 −4
Changes for core/modules/file/file.module: 4 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -769,7 +769,7 @@ function file_cron() {
 *   - source: Path to the file before it is moved.
 *   - destination: Path to the file after it is moved (same as 'uri').
 */
function file_save_upload($form_field_name, array &$form_state, $validators = array(), $destination = FALSE, $delta = NULL, $replace = FILE_EXISTS_RENAME) {
function file_save_upload($form_field_name, $validators = array(), $destination = FALSE, $delta = NULL, $replace = FILE_EXISTS_RENAME) {
  $user = \Drupal::currentUser();
  static $upload_cache;

@@ -927,7 +927,7 @@ function file_save_upload($form_field_name, array &$form_state, $validators = ar
      else {
        $message .= ' ' . array_pop($errors);
      }
      form_set_error($form_field_name, $form_state, $message);
      drupal_set_message($message, 'error');
      $files[$i] = FALSE;
      continue;
    }
@@ -937,7 +937,7 @@ function file_save_upload($form_field_name, array &$form_state, $validators = ar
    // operations.
    $file->uri = $file->destination;
    if (!drupal_move_uploaded_file($file_info->getRealPath(), $file->getFileUri())) {
      form_set_error($form_field_name, $form_state, t('File upload error. Could not move uploaded file.'));
      drupal_set_message(t('File upload error. Could not move uploaded file.'), 'error');
      watchdog('file', 'Upload error. Could not move uploaded file %file to destination %destination.', array('%file' => $file->filename, '%destination' => $file->uri));
      $files[$i] = FALSE;
      continue;
@@ -1496,7 +1496,7 @@ function file_managed_file_save_upload($element, array &$form_state) {
  $files_uploaded = $element['#multiple'] && count(array_filter($file_upload)) > 0;
  $files_uploaded |= !$element['#multiple'] && !empty($file_upload);
  if ($files_uploaded) {
    if (!$files = file_save_upload($upload_name, $form_state, $element['#upload_validators'], $destination)) {
    if (!$files = file_save_upload($upload_name, $element['#upload_validators'], $destination)) {
      watchdog('file', 'The file upload failed. %upload', array('%upload' => $upload_name));
      form_set_error($upload_name, $form_state, t('Files in the !name field were unable to be uploaded.', array('!name' => $element['#title'])));
      return array();
+1 −1
Changes for core/modules/file/tests/file_test/lib/Drupal/file_test/Form/FileTestForm.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -101,7 +101,7 @@ public function submitForm(array &$form, array &$form_state) {
      $validators['file_validate_extensions'] = array($form_state['values']['extensions']);
    }

    $file = file_save_upload('file_test_upload', $form_state, $validators, $destination, 0, $form_state['values']['file_test_replace']);
    $file = file_save_upload('file_test_upload', $validators, $destination, 0, $form_state['values']['file_test_replace']);
    if ($file) {
      $form_state['values']['file_test_upload'] = $file;
      drupal_set_message(t('File @filepath was uploaded.', array('@filepath' => $file->getFileUri())));
Loading