clearErrors example


  }

  /** * @covers ::setErrorByName * * @dataProvider providerTestSetErrorByName */
  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 [
      
$this->assertCount(1, $nestedErrorsAsArray);
        $this->assertSame($nestedError$nestedErrorsAsArray[0]);
    }

    public function testClearErrors()
    {
        $this->form->addError(new FormError('Error 1'));
        $this->form->addError(new FormError('Error 2'));

        $this->assertCount(2, $this->form->getErrors());

        $this->form->clearErrors();

        $this->assertCount(0, $this->form->getErrors());
    }

    public function testClearErrorsShallow()
    {
        $this->form->addError($error1 = new FormError('Error 1'));
        $this->form->addError($error2 = new FormError('Error 2'));

        $childForm = $this->getBuilder('Child')->getForm();
        $childForm->addError(new FormError('Nested Error'));
        
    $form_state->setUserInput($form_state->getValues());

    $form_state->setProgrammed();

    $form_id = $this->getFormId($form_arg$form_state);
    $form = $this->retrieveForm($form_id$form_state);
    // Programmed forms are always submitted.     $form_state->setSubmitted();

    // Reset form validation.     $form_state->setValidationEnforced();
    $form_state->clearErrors();

    $this->prepareForm($form_id$form$form_state);
    $this->processForm($form_id$form$form_state);
  }

  /** * {@inheritdoc} */
  public function retrieveForm($form_id, FormStateInterface &$form_state) {
    // Record the $form_id.     $form_state->addBuildInfo('form_id', $form_id);

    
// Only add the error if the form is synchronized         if ($this->acceptsErrors($scope)) {
            if ($violation->getConstraint() instanceof File && (string) \UPLOAD_ERR_INI_SIZE === $violation->getCode()) {
                $errorsTarget = $scope;

                while (null !== $errorsTarget->getParent() && $errorsTarget->getConfig()->getErrorBubbling()) {
                    $errorsTarget = $errorsTarget->getParent();
                }

                $errors = $errorsTarget->getErrors();
                $errorsTarget->clearErrors();

                foreach ($errors as $error) {
                    if (!$error instanceof FileUploadError) {
                        $errorsTarget->addError($error);
                    }
                }
            }

            $message = $violation->getMessage();
            $messageTemplate = $violation->getMessageTemplate();

            
->getMock();
    $this->root = dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__)), 2);

    $this->formBuilder = new FormBuilder($this->formValidator, $this->formSubmitter, $this->formCache, $this->moduleHandler, $this->eventDispatcher, $this->requestStack, $this->classResolver, $this->elementInfo, $this->themeManager, $this->csrfToken);
  }

  /** * {@inheritdoc} */
  protected function tearDown(): void {
    Html::resetSeenIds();
    (new FormState())->clearErrors();
  }

  /** * Provides a mocked form object. * * @param string $form_id * The form ID to be used. * @param mixed $expected_form * (optional) If provided, the expected form response for buildForm() to * return. Defaults to NULL. * @param int $count * (optional) The number of times the form is expected to be built. Defaults * to 1. * * @return \PHPUnit\Framework\MockObject\MockObject|\Drupal\Core\Form\FormInterface * The mocked form object. */
$form_state->setUserInput($edit);
    $form_state->setFormObject(new StubForm($form_id$form));

    \Drupal::formBuilder()->prepareForm($form_id$form$form_state);

    \Drupal::formBuilder()->processForm($form_id$form$form_state);

    $errors = $form_state->getErrors();

    // Clear errors and messages.     \Drupal::messenger()->deleteAll();
    $form_state->clearErrors();

    // Return the processed form together with form_state and errors     // to allow the caller low-level access to the form.     return [$form$form_state$errors];
  }

}

  protected function setupForm(FormStateInterface $form_state, FormBuilderInterface $form_builder) {
    $form_id = $form_builder->getFormId($this$form_state);
    $form = $form_builder->retrieveForm($form_id$form_state);
    $form_state->setValidationEnforced();
    $form_state->clearErrors();
    $form_builder->prepareForm($form_id$form$form_state);
    $form_builder->processForm($form_id$form$form_state);
    return $form_builder->retrieveForm($form_id$form_state);
  }

  /** * {@inheritdoc} */
  public function getFormId() {
    return 'test_datetime_elements';
  }

  
$this->decoratedFormState->setError($element$message)
      ->shouldBeCalled();

    $this->assertSame($this->formStateDecoratorBase, $this->formStateDecoratorBase->setError($element$message));
  }

  /** * @covers ::clearErrors */
  public function testClearErrors() {
    $this->decoratedFormState->clearErrors()
      ->shouldBeCalled();

    $this->formStateDecoratorBase->clearErrors();
  }

  /** * @covers ::getError */
  public function testGetError() {
    $element = [
      '#foo' => 'bar',
    ];

  public function setError(array &$element$message = '') {
    $this->decoratedFormState->setError($element$message);

    return $this;
  }

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

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

  /** * {@inheritdoc} */
return new FormErrorIterator($this$errors);
    }

    public function clearErrors(bool $deep = false)static
    {
        $this->errors = [];

        if ($deep) {
            // Clear errors from children             foreach ($this as $child) {
                if ($child instanceof ClearableErrorsInterface) {
                    $child->clearErrors(true);
                }
            }
        }

        return $this;
    }

    public function all(): array
    {
        return iterator_to_array($this->children);
    }

    
Home | Imprint | This part of the site doesn't use cookies.