doSubmitForm example

/** * Tests the programmatic form submission workflow. */
  public function testSubmissionWorkflow() {
    // Backup the current batch status and reset it to avoid conflicts while     // processing the dummy form submit handler.     $current_batch = $batch =& batch_get();
    $batch = [];

    // Test that a programmatic form submission is rejected when a required     // textfield is omitted and correctly processed when it is provided.     $this->doSubmitForm([], FALSE);
    $this->doSubmitForm(['textfield' => 'test 1'], TRUE);
    $this->doSubmitForm([], FALSE);
    $this->doSubmitForm(['textfield' => 'test 2'], TRUE);

    // Test that a programmatic form submission can turn on and off checkboxes     // which are, by default, checked.     $this->doSubmitForm(['textfield' => 'dummy value', 'checkboxes' => [1 => 1, 2 => 2]], TRUE);
    $this->doSubmitForm(['textfield' => 'dummy value', 'checkboxes' => [1 => 1, 2 => NULL]], TRUE);
    $this->doSubmitForm(['textfield' => 'dummy value', 'checkboxes' => [1 => NULL, 2 => 2]], TRUE);
    $this->doSubmitForm(['textfield' => 'dummy value', 'checkboxes' => [1 => NULL, 2 => NULL]], TRUE);

    
      // 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;
        }
      }

      // Don't rebuild or cache form submissions invoked via self::submitForm().
$this->unroutedUrlAssembler = $this->createMock(UnroutedUrlAssemblerInterface::class);
  }

  /** * @covers ::doSubmitForm */
  public function testHandleFormSubmissionNotSubmitted() {
    $form_submitter = $this->getFormSubmitter();
    $form = [];
    $form_state = new FormState();

    $return = $form_submitter->doSubmitForm($form$form_state);
    $this->assertFalse($form_state->isExecuted());
    $this->assertNull($return);
  }

  /** * @covers ::doSubmitForm */
  public function testHandleFormSubmissionNoRedirect() {
    $form_submitter = $this->getFormSubmitter();
    $form = [];
    $form_state = (new FormState())
      
Home | Imprint | This part of the site doesn't use cookies.