isValidationComplete example


  protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
    if (!$form_state->isValidationComplete()) {
      // Only do something once form validation is complete.       return;
    }
    /** @var \Drupal\workflows\WorkflowInterface $entity */
    $values = $form_state->getValues();
    $entity->getTypePlugin()->addState($values['id']$values['label']);
  }

  /** * {@inheritdoc} */
  

  public static function getGeneratedAllowedHtmlValue(array &$element$input, FormStateInterface $form_state): string {
    if ($form_state->isValidationComplete()) {
      $validated_format = $form_state->get('ckeditor5_validated_pair')->getFilterFormat();
      $configuration = $validated_format->filters()->get('filter_html')->getConfiguration();
      return $configuration['settings']['allowed_html'];
    }
    else {
      if ($input !== FALSE) {
        return $input;
      }
      return $element['#default_value'];
    }
  }

  
/** * Tests the 'validation_complete' $form_state flag. * * @covers ::validateForm * @covers ::finalizeValidation */
  public function testValidationComplete() {
    $form_validator = new FormValidator(new RequestStack()$this->getStringTranslationStub()$this->csrfToken, $this->logger, $this->formErrorHandler);

    $form = [];
    $form_state = new FormState();
    $this->assertFalse($form_state->isValidationComplete());
    $form_validator->validateForm('test_form_id', $form$form_state);
    $this->assertTrue($form_state->isValidationComplete());
  }

  /** * Tests the 'must_validate' $form_state flag. * * @covers ::validateForm */
  public function testPreventDuplicateValidation() {
    $form_validator = $this->getMockBuilder('Drupal\Core\Form\FormValidator')
      

  protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
    if (!$form_state->isValidationComplete()) {
      // Only do something once form validation is complete.       return;
    }
    /** @var \Drupal\workflows\WorkflowInterface $entity */
    $values = $form_state->getValues();
    $entity->getTypePlugin()->addTransition($values['id']$values['label']array_filter($values['from'])$values['to']);
  }

  /** * {@inheritdoc} */
  

  protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
    if (!$form_state->isValidationComplete()) {
      // Only do something once form validation is complete.       return;
    }
    /** @var \Drupal\workflows\WorkflowInterface $entity */
    $values = $form_state->getValues();
    $entity->getTypePlugin()->setStateLabel($values['id']$values['label']);
  }

  /** * {@inheritdoc} */
  
public static function layoutBuilderElementGetKeys(array $element, FormStateInterface $form_state, &$form) {
    $form['#layout_builder_element_keys'] = $element['#array_parents'];
    return $element;
  }

  /** * {@inheritdoc} */
  public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) {
    // @todo This isn't resilient to being set twice, during validation and     // save https://www.drupal.org/project/drupal/issues/2833682.     if (!$form_state->isValidationComplete()) {
      return;
    }

    $items->setValue($this->getSectionStorage($form_state)->getSections());
  }

  /** * Gets the section storage. * * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state. * * @return \Drupal\layout_builder\SectionStorageInterface * The section storage loaded from the tempstore. */
/** * @covers ::isValidationComplete * * @dataProvider providerSingleBooleanArgument * * @param bool $complete * Any valid value for * \Drupal\Core\Form\FormStateInterface::isValidationComplete()'s return * value. */
  public function testIsValidationComplete($complete) {
    $this->decoratedFormState->isValidationComplete()
      ->willReturn($complete)
      ->shouldBeCalled();

    $this->assertSame($complete$this->formStateDecoratorBase->isValidationComplete());
  }

  /** * @covers ::loadInclude * * @dataProvider providerLoadInclude * * @param string|false $expected * Any valid value for \Drupal\Core\Form\FormStateInterface::loadInclude()'s * return value. * @param string $module * Any valid value for \Drupal\Core\Form\FormStateInterface::loadInclude()'s * $module argument. * @param string $type * Any valid value for \Drupal\Core\Form\FormStateInterface::loadInclude()'s * $type argument. * @param string|null $name * Any valid value for \Drupal\Core\Form\FormStateInterface::loadInclude()'s * $name argument. */
/** * {@inheritdoc} */
  public function validateForm($form_id, &$form, FormStateInterface &$form_state) {
    // If this form is flagged to always validate, ensure that previous runs of     // validation are ignored.     if ($form_state->isValidationEnforced()) {
      $form_state->setValidationComplete(FALSE);
    }

    // If this form has completed validation, do not validate again.     if ($form_state->isValidationComplete()) {
      return;
    }

    // If the session token was set by self::prepareForm(), ensure that it     // matches the current user's session. This is duplicate to code in     // FormBuilder::doBuildForm() but left to protect any custom form handling     // code.     if (isset($form['#token'])) {
      if (!$this->csrfToken->validate($form_state->getValue('form_token')$form['#token']) || $form_state->hasInvalidToken()) {
        $this->setInvalidTokenError($form_state);

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

  /** * {@inheritdoc} */
  public function setErrorByName($name$message = '') {
    if ($this->isValidationComplete()) {
      throw new \LogicException('Form errors cannot be set after form validation has finished.');
    }

    $errors = $this->getErrors();
    if (!isset($errors[$name])) {
      $record = TRUE;
      $limit_validation_errors = $this->getLimitValidationErrors();
      if ($limit_validation_errors !== NULL) {
        $record = FALSE;
        foreach ($limit_validation_errors as $section) {
          // Exploding by '][' reconstructs the element's #parents. If the

  public function setValidationComplete($validation_complete = TRUE) {
    $this->decoratedFormState->setValidationComplete($validation_complete);

    return $this;
  }

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

  /** * {@inheritdoc} */
  public function loadInclude($module$type$name = NULL) {
    return $this->decoratedFormState->loadInclude($module$type$name);
  }

  /** * {@inheritdoc} */

  protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
    if (!$form_state->isValidationComplete()) {
      // Only do something once form validation is complete.       return;
    }
    /** @var \Drupal\workflows\WorkflowInterface $entity */
    $values = $form_state->getValues();
    $form_state->set('created_transition', FALSE);
    $entity->getTypePlugin()->setTransitionLabel($values['id']$values['label']);
    $entity->getTypePlugin()->setTransitionFromStates($values['id']array_filter($values['from']));
  }

  /** * {@inheritdoc} */
Home | Imprint | This part of the site doesn't use cookies.