validateForm example

return (bool) $this->entityTypeManager
      ->getStorage('filter_format')
      ->getQuery()
      ->condition('format', $format_id)
      ->execute();
  }

  /** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form$form_state);

    // @todo Move trimming upstream.     $format_format = trim($form_state->getValue('format'));
    $format_name = trim($form_state->getValue('name'));

    // Ensure that the values to be saved later are exactly the ones validated.     $form_state->setValueForElement($form['format']$format_format);
    $form_state->setValueForElement($form['name']$format_name);

    $format_exists = $this->entityTypeManager
      ->getStorage('filter_format')
      
      return;
    }
    /** @var \Drupal\workflows\WorkflowInterface $entity */
    $values = $form_state->getValues();
    $entity->getTypePlugin()->addState($values['id']$values['label']);
  }

  /** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form$form_state);
    /** @var \Drupal\workflows\WorkflowTypeInterface $workflow_type */
    $workflow = $this->entity;
    $workflow_type = $workflow->getTypePlugin();

    if ($workflow_type->hasFormClass(StateInterface::PLUGIN_FORM_KEY)) {
      $subform_state = SubformState::createForSubform($form['type_settings']$form$form_state);
      $this->pluginFormFactory
        ->createInstance($workflow_type, StateInterface::PLUGIN_FORM_KEY)
        ->validateConfigurationForm($form['type_settings']$subform_state);
    }
  }

  

      '#description' => $this->t('How to treat existing translations when automatically updating the interface translations.'),
    ];

    return parent::buildForm($form$form_state);
  }

  /** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form$form_state);

    if (empty($form['#translation_directory']) && $form_state->getValue('use_source') == LOCALE_TRANSLATION_USE_SOURCE_LOCAL) {
      $form_state->setErrorByName('use_source', $this->t('You have selected local translation source, but no <a href=":url">Interface translation directory</a> was configured.', [':url' => Url::fromRoute('system.file_system_settings')->toString()]));
    }
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state->getValues();

    
'#title' => $this->t('Make this the default form'),
      '#default_value' => $default_form === $contact_form->id(),
    ];

    return $form;
  }

  /** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form$form_state);

    // Validate and each email recipient.     $recipients = explode(',', $form_state->getValue('recipients'));

    foreach ($recipients as &$recipient) {
      $recipient = trim($recipient);
      if (!$this->emailValidator->isValid($recipient)) {
        $form_state->setErrorByName('recipients', $this->t('%recipient is an invalid email address.', ['%recipient' => $recipient]));
      }
    }
    $form_state->setValue('recipients', $recipients);
    

  protected function actions(array $form, FormStateInterface $form_state) {
    $actions = parent::actions($form$form_state);
    $actions['submit']['#value'] = $this->t('Save content type');
    return $actions;
  }

  /** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form$form_state);

    $id = trim($form_state->getValue('type'));
    // '0' is invalid, since elsewhere we check it using empty().     if ($id == '0') {
      $form_state->setErrorByName('type', $this->t("Invalid machine-readable name. Enter a name other than %invalid.", ['%invalid' => $id]));
    }
  }

  /** * {@inheritdoc} */
  

  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')
      ->setConstructorArgs([new RequestStack()$this->getStringTranslationStub()$this->csrfToken, $this->logger, $this->formErrorHandler])
      
protected function actions(array $form, FormStateInterface $form_state) {
    $actions = parent::actions($form$form_state);
    $actions['submit']['#value'] = $this->t('Save block');
    $actions['delete']['#title'] = $this->t('Remove block');
    return $actions;
  }

  /** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form$form_state);

    $form_state->setValue('weight', (int) $form_state->getValue('weight'));
    // The Block Entity form puts all block plugin form elements in the     // settings form element, so just pass that to the block for validation.     $this->getPluginForm($this->entity->getPlugin())->validateConfigurationForm($form['settings'], SubformState::createForSubform($form['settings']$form$form_state));
    $this->validateVisibility($form$form_state);
  }

  /** * Helper function to independently validate the visibility UI. * * @param array $form * A nested array form elements comprising the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */

  public function settingsAjaxSubmit($form, FormStateInterface $form_state) {
    $form_state->set('layout_plugin', NULL);
    $form_state->setRebuild();
  }

  /** * Overrides \Drupal\field_ui\Form\EntityDisplayFormBase::validateForm(). */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form$form_state);

    $layout_plugin = $this->getLayout($this->getEntity()$form_state);
    if ($layout_plugin instanceof PluginFormInterface) {
      $subform_state = SubformState::createForSubform($form['field_layouts']['settings_wrapper']['layout_settings']$form$form_state);
      $layout_plugin->validateConfigurationForm($form['field_layouts']['settings_wrapper']['layout_settings']$subform_state);
    }
  }

  /** * Overrides \Drupal\field_ui\Form\EntityDisplayFormBase::submitForm(). */
  

      $form['image_toolkit_settings'][$id] += $toolkit->buildConfigurationForm([]$form_state);
    }

    return parent::buildForm($form$form_state);
  }

  /** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form$form_state);

    // Call the form validation handler for each of the toolkits.     foreach ($this->availableToolkits as $toolkit) {
      $toolkit->validateConfigurationForm($form$form_state);
    }
  }

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


    // More specific mapping tests can be found in ViolationMapperTest     public function testMapViolation()
    {
        $violation = new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, 'data', null, null, null, new FormConstraint());
        $form = new Form(new FormConfigBuilder('street', null, new EventDispatcher()));
        $form->submit(null);

        $validator = new DummyValidator($violation);
        $listener = new ValidationListener($validatornew ViolationMapper());
        $listener->validateForm(new FormEvent($form, null));

        $this->assertCount(1, $form->getErrors());
        $this->assertSame($violation$form->getErrors()[0]->getCause());
    }

    public function testMapViolationAllowsNonSyncIfInvalid()
    {
        $violation = new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, 'data', null, null, FormConstraint::NOT_SYNCHRONIZED_ERROR, new FormConstraint());
        $form = new SubmittedNotSynchronizedForm(new FormConfigBuilder('street', null, new EventDispatcher()));

        $validator = new DummyValidator($violation);
        
    if ($form_state->isProcessingInput()) {
      // Form values for programmed form submissions typically do not include a       // value for the submit button. But without a triggering element, a       // potentially existing #limit_validation_errors property on the primary       // submit button is not taken account. Therefore, check whether there is       // exactly one submit button in the form, and if so, automatically use it       // as triggering_element.       $buttons = $form_state->getButtons();
      if ($form_state->isProgrammed() && !$form_state->getTriggeringElement() && count($buttons) == 1) {
        $form_state->setTriggeringElement(reset($buttons));
      }
      $this->formValidator->validateForm($form_id$form$form_state);

      // If there are no errors and the form is not rebuilding, submit the form.       if (!$form_state->isRebuilding() && !FormState::hasAnyErrors()) {
        $submit_response = $this->formSubmitter->doSubmitForm($form$form_state);
        // If this form was cached, delete it from the cache after submission.         if ($form_state->isCached()) {
          $this->deleteCache($form['#build_id']);
        }
        // If the form submission directly returned a response, return it now.         if ($submit_response) {
          return $submit_response;
        }


  /** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $child_type = $form_state->getValue('book_child_type');
    if ($form_state->isValueEmpty(['book_allowed_types', $child_type])) {
      $form_state->setErrorByName('book_child_type', $this->t('The content type for the %add-child link must be one of those selected as an allowed book outline type.', ['%add-child' => $this->t('Add child page')]));
    }

    parent::validateForm($form$form_state);
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $allowed_types = array_filter($form_state->getValue('book_allowed_types'));
    // We need to save the allowed types in an array ordered by machine_name so     // that we can save them in the correct order if node type changes.     // @see book_node_type_update().     sort($allowed_types);
    
public function exists($id) {
    $entity = $this->entityTypeManager->getStorage('search_page')->getQuery()
      ->condition('id', $id)
      ->execute();
    return (bool) $entity;
  }

  /** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form$form_state);

    // Ensure each path is unique.     $path = $this->entityTypeManager->getStorage('search_page')->getQuery()
      ->condition('path', $form_state->getValue('path'))
      ->condition('id', $form_state->getValue('id'), '<>')
      ->execute();
    if ($path) {
      $form_state->setErrorByName('path', $this->t('The search page path must be unique.'));
    }

    if ($this->plugin instanceof PluginFormInterface) {
      
if ($this->entity->isLocked()) {
      $actions['submit']['#access'] = FALSE;
      $actions['cancel']['#access'] = FALSE;
    }
    return $actions;
  }

  /** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form$form_state);

    $view = $this->entity;
    if ($view->isLocked()) {
      $form_state->setErrorByName('', $this->t('Changes cannot be made to a locked view.'));
    }
    foreach ($view->getExecutable()->validate() as $display_errors) {
      foreach ($display_errors as $error) {
        $form_state->setErrorByName('', $error);
      }
    }
  }

  

  protected function getSourceSubFormState(array $form, FormStateInterface $form_state) {
    return SubformState::createForSubform($form['source_dependent']['source_configuration']$form$form_state)
      ->set('operation', $this->operation)
      ->set('type', $this->entity);
  }

  /** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form$form_state);

    if (isset($form['source_dependent']['source_configuration'])) {
      // Let the selected plugin validate its settings.       $this->entity->getSource()->validateConfigurationForm($form['source_dependent']['source_configuration']$this->getSourceSubFormState($form$form_state));
    }
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    
Home | Imprint | This part of the site doesn't use cookies.