cleanValues example


    }

    return $form;
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $form_state->cleanValues();
    // This won't have a proper JSON header, but Drupal doesn't check for that     // anyway so this is fine until it's replaced with a JsonResponse.     print Json::encode($form_state->getValues());
    exit;
  }

}
$this->assertSame($form_state->getCleanValueKeys()['form_id', 'form_token', 'form_build_id', 'op', 'value_to_clean']);
    return $form_state;
  }

  /** * @depends testAddCleanValueKey * * @covers ::cleanValues */
  public function testCleanValues($form_state) {
    $form_state->setValue('value_to_keep', 'magic_ponies');
    $this->assertSame($form_state->cleanValues()->getValues()['value_to_keep' => 'magic_ponies']);
  }

  /** * @covers ::setValues * @covers ::getValues */
  public function testGetValues() {
    $values = [
      'foo' => 'bar',
    ];
    $form_state = new FormState();
    
/** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form$form_state);

    $config_key = $form_state->getValue('config_key');
    $this->editableConfig = [$config_key];
    $config = $this->config($config_key);

    // Exclude unnecessary elements before saving.     $form_state->cleanValues();
    $form_state->unsetValue('var');
    $form_state->unsetValue('config_key');

    $values = $form_state->getValues();

    // If the user uploaded a new logo or favicon, save it to a permanent location     // and use it in place of the default theme-provided file.     $default_scheme = $this->config('system.file')->get('default_scheme');
    try {
      if (!empty($values['logo_upload'])) {
        $filename = $this->fileSystem->copy($values['logo_upload']->getFileUri()$default_scheme . '://');
        
$form['submit'] = [
      '#type' => 'submit',
      '#value' => t('Submit'),
    ];
    return $form;
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $form_state->cleanValues();
    print t('You WIN!');
    exit;
  }

}

  public function validateForm(array &$form, FormStateInterface $form_state) {
    // The image effect configuration is stored in the 'data' key in the form,     // pass that through for validation.     $this->imageEffect->validateConfigurationForm($form['data'], SubformState::createForSubform($form['data']$form$form_state));
  }

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

    // The image effect configuration is stored in the 'data' key in the form,     // pass that through for submission.     $this->imageEffect->submitConfigurationForm($form['data'], SubformState::createForSubform($form['data']$form$form_state));

    $this->imageEffect->setWeight($form_state->getValue('weight'));
    if (!$this->imageEffect->getUuid()) {
      $this->imageStyle->addImageEffect($this->imageEffect->getConfiguration());
    }
    $this->imageStyle->save();

    
$this->decoratedFormState->addCleanValueKey($key)
      ->shouldBeCalled();

    $this->assertSame($this->formStateDecoratorBase, $this->formStateDecoratorBase->addCleanValueKey($key));
  }

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

    $this->assertSame($this->formStateDecoratorBase, $this->formStateDecoratorBase->cleanValues());
  }

}

/** * Provides a non-abstract version of the class under test. */
class NonAbstractFormStateDecoratorBase extends FormStateDecoratorBase {

  
public function submitForm(array &$form, FormStateInterface $form_state) {
    $admin = $form_state->getValue('administer_users');

    if (!\Drupal::config('user.settings')->get('verify_mail') || $admin) {
      $pass = $form_state->getValue('pass');
    }
    else {
      $pass = \Drupal::service('password_generator')->generate();
    }

    // Remove unneeded values.     $form_state->cleanValues();

    $form_state->setValue('pass', $pass);
    $form_state->setValue('init', $form_state->getValue('mail'));

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

  /** * {@inheritdoc} */
  public function save(array $form, FormStateInterface $form_state) {
    
public function testFormCacheDeletionCached() {
    $form_id = 'test_form_id';
    $form_build_id = $this->randomMachineName();

    $expected_form = $form_id();
    $expected_form['#build_id'] = $form_build_id;
    $form_arg = $this->getMockForm($form_id$expected_form);
    $form_arg->expects($this->once())
      ->method('submitForm')
      ->willReturnCallback(function Darray &$form, FormStateInterface $form_state) {
        // Mimic EntityForm by cleaning the $form_state upon submit.         $form_state->cleanValues();
      });

    $this->formCache->expects($this->once())
      ->method('deleteCache')
      ->with($form_build_id);

    $form_state = new FormState();
    $form_state->setRequestMethod('POST');
    $form_state->setCached();
    $this->simulateFormSubmission($form_id$form_arg$form_state);
  }

  
/** * Helper function to clean a value on an element. */
  public static function cleanValue(&$element, FormStateInterface $form_state, &$complete_form) {
    $form_state->addCleanValueKey('wine');
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $form_state->cleanValues();
    // This won't have a proper JSON header, but Drupal doesn't check for that     // anyway so this is fine until it's replaced with a JsonResponse.     print Json::encode($form_state->getValues());
    exit;
  }

}

  public function addCleanValueKey($cleanValueKey) {
    $this->decoratedFormState->addCleanValueKey($cleanValueKey);

    return $this;
  }

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

    return $this;
  }

}

  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Remove button and internal Form API values from submitted values.     $form_state->cleanValues();
    $this->entity = $this->buildEntity($form$form_state);
  }

  /** * {@inheritdoc} */
  public function save(array $form, FormStateInterface $form_state) {
    return $this->entity->save();
  }

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