hasAnyErrors example

/** * {@inheritdoc} */
  public function getRedirect() {
    return $this->decoratedFormState->getRedirect();
  }

  /** * {@inheritdoc} */
  public static function hasAnyErrors() {
    return FormState::hasAnyErrors();
  }

  /** * {@inheritdoc} */
  public function setErrorByName($name$message = '') {
    $this->decoratedFormState->setErrorByName($name$message);

    return $this;
  }

  

  public function testSetErrorByName($limit_validation_errors$expected_errors) {
    $form_state = new FormState();
    $form_state->setLimitValidationErrors($limit_validation_errors);
    $form_state->clearErrors();

    $form_state->setErrorByName('test', 'Fail 1');
    $form_state->setErrorByName('test', 'Fail 2');
    $form_state->setErrorByName('options');

    $this->assertSame(!empty($expected_errors)$form_state::hasAnyErrors());
    $this->assertSame($expected_errors$form_state->getErrors());
  }

  public function providerTestSetErrorByName() {
    return [
      // Only validate the 'options' element.       [[['options']]['options' => '']],
      // Do not limit a validation, ensure the first error is returned       // for the 'test' element.       [NULL, ['test' => 'Fail 1', 'options' => '']],
      // Limit all validation.

  public function ajaxSubmit(array &$form, FormStateInterface $form_state) {
    if ($form_state->hasAnyErrors()) {
      $form['status_messages'] = [
        '#type' => 'status_messages',
        '#weight' => -1000,
      ];
      $form['#sorted'] = FALSE;
      $response = new AjaxResponse();
      $response->addCommand(new ReplaceCommand('[data-drupal-selector="' . $form['#attributes']['data-drupal-selector'] . '"]', $form));
    }
    else {
      $response = $this->successfulAjaxSubmit($form$form_state);
    }
    
    // which means the request method matches the form method, and if not,     // there's some other error, so it's ok if an exception is thrown.     if ($form_state->isMethodType('POST')) {
      $form_state->setCached();
    }

    // \Drupal\Component\Utility\Html::getUniqueId() maintains a cache of     // element IDs it has seen, so it can prevent duplicates. We want to be     // sure we reset that cache when a form is processed, so scenarios that     // result in the form being built behind the scenes and again for the     // browser don't increment all the element IDs needlessly.     if (!FormState::hasAnyErrors()) {
      // We only reset HTML ID's when there are no validation errors as this can       // cause ID collisions with other forms on the page otherwise.       Html::resetSeenIds();
    }

    // If only parts of the form will be returned to the browser (e.g., Ajax or     // RIA clients), or if the form already had a new build ID regenerated when     // it was retrieved from the form cache, reuse the existing #build_id.     // Otherwise, a new #build_id is generated, to not clobber the previous     // build's data in the form cache; also allowing the user to go back to an     // earlier build, make changes, and re-submit.
public function updateFormCallback(array &$form, FormStateInterface $form_state) {
    $triggering_element = $form_state->getTriggeringElement();
    $wrapper_id = $triggering_element['#ajax']['wrapper'];
    $added_media = $form_state->get('media');

    $response = new AjaxResponse();

    // When the source field input contains errors, replace the existing form to     // let the user change the source field input. If the user input is valid,     // the entire modal is replaced with the second step of the form to show the     // form fields for each media item.     if ($form_state::hasAnyErrors()) {
      $response->addCommand(new ReplaceCommand('#media-library-add-form-wrapper', $form));
      return $response;
    }

    // Check if the remove button is clicked.     if (end($triggering_element['#parents']) === 'remove_button') {
      // When the list of added media is empty, return to the media library and       // shift focus back to the first tabbable element (which should be the       // source field).       if (empty($added_media)) {
        $response->addCommand(new ReplaceCommand('#media-library-add-form-wrapper', $this->buildMediaLibraryUi($form_state)));
        

  public function form_test_storage_page_cache_rebuild($form, FormStateInterface $form_state) {
    $form_state->setRebuild();
  }

  /** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    // Test using form cache when re-displaying a form due to validation     // errors.     if ($form_state->hasAnyErrors()) {
      $form_state->setCached();
    }
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Nothing must happen.   }

}

  public function validateUploadElement(array $element, FormStateInterface $form_state) {
    if ($form_state::hasAnyErrors()) {
      // When an error occurs during uploading files, remove all files so the       // user can re-upload the files.       $element['#value'] = [];
    }
    $values = $form_state->getValue('upload', []);
    if (count($values['fids']) > $element['#cardinality'] && $element['#cardinality'] !== FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
      $form_state->setError($element$this->t('A maximum of @count files can be uploaded.', [
        '@count' => $element['#cardinality'],
      ]));
      $form_state->setValue('upload', []);
      $element['#value'] = [];
    }
Home | Imprint | This part of the site doesn't use cookies.