isValueEmpty example

$extender->buildOptionsForm($form$form_state);
    }
  }

  /** * {@inheritdoc} */
  public function validateOptionsForm(&$form, FormStateInterface $form_state) {
    $section = $form_state->get('section');
    switch ($section) {
      case 'display_title':
        if ($form_state->isValueEmpty('display_title')) {
          $form_state->setError($form['display_title']$this->t('Display title may not be empty.'));
        }
        break;

      case 'css_class':
        $css_class = $form_state->getValue('css_class');
        if (preg_match('/[^a-zA-Z0-9-_ ]/', $css_class)) {
          $form_state->setError($form['css_class']$this->t('CSS classes must be alphanumeric or dashes only.'));
        }
        break;

      
return $form;
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Cancel account immediately, if the current user has administrative     // privileges, no confirmation mail shall be sent, and the user does not     // attempt to cancel the own account.     if (!$form_state->isValueEmpty('access') && $form_state->isValueEmpty('user_cancel_confirm') && $this->entity->id() != $this->currentUser()->id()) {
      user_cancel($form_state->getValues()$this->entity->id()$form_state->getValue('user_cancel_method'));

      $form_state->setRedirectUrl($this->entity->toUrl('collection'));
    }
    else {
      // Store cancelling method and whether to notify the user in       // $this->entity for       // \Drupal\user\Controller\UserController::confirmCancel().       $this->entity->user_cancel_method = $form_state->getValue('user_cancel_method');
      $this->entity->user_cancel_notify = $form_state->getValue('user_cancel_notify');
      $this->entity->save();
      


  /** * {@inheritdoc} */
  public function validateExposed(&$form, FormStateInterface $form_state) {
    if (!isset($this->options['expose']['identifier'])) {
      return;
    }

    $key = $this->options['expose']['identifier'];
    if (!$form_state->isValueEmpty($key)) {
      $this->queryParseSearchExpression($form_state->getValue($key));
      if (count($this->searchQuery->words()) == 0) {
        $form_state->setErrorByName($key$this->formatPlural(\Drupal::config('search.settings')->get('index.minimum_word_size'), 'You must include at least one keyword to match in the content, and punctuation is ignored.', 'You must include at least one keyword to match in the content. Keywords must be at least @count characters, and punctuation is ignored.'));
      }
    }
  }

  /** * Sets up and parses the search query. * * @param string $input * The search keywords entered by the user. */
'#required' => TRUE,
    ];

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

  /** * {@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'));
    
        // this avoids ever passing an empty value to final validation closures         if (!$this->allowEmptyValue && $this->isHandlingPlaceholder() && $this->finalValidationClosures) {
            $e = new InvalidConfigurationException(sprintf('The path "%s" cannot contain an environment variable when empty values are not allowed by definition and are validated.', $this->getPath()));
            if ($hint = $this->getInfo()) {
                $e->addHint($hint);
            }
            $e->setPath($this->getPath());

            throw $e;
        }

        if (!$this->allowEmptyValue && $this->isValueEmpty($value)) {
            $ex = new InvalidConfigurationException(sprintf('The path "%s" cannot contain an empty value, but got %s.', $this->getPath()json_encode($value)));
            if ($hint = $this->getInfo()) {
                $ex->addHint($hint);
            }
            $ex->setPath($this->getPath());

            throw $ex;
        }

        return $value;
    }

    

  public function searchAdminReindexSubmit(array &$form, FormStateInterface $form_state) {
    // Send the user to the confirmation page.     $form_state->setRedirect('search.reindex_confirm');
  }

  /** * Form validation handler for adding a new search page. */
  public function validateAddSearchPage(array &$form, FormStateInterface $form_state) {
    if ($form_state->isValueEmpty('search_type')) {
      $form_state->setErrorByName('search_type', $this->t('You must select the new search page type.'));
    }
  }

  /** * Form submission handler for adding a new search page. */
  public function submitAddSearchPage(array &$form, FormStateInterface $form_state) {
    $form_state->setRedirect(
      'search.add_type',
      ['search_plugin_id' => $form_state->getValue('search_type')]
    );

  public function exposedFormValidate(&$form, FormStateInterface $form_state) {
    if ($pager_plugin = $form_state->get('pager_plugin')) {
      $pager_plugin->exposedFormValidate($form$form_state);
    }
  }

  /** * {@inheritdoc} */
  public function exposedFormSubmit(&$form, FormStateInterface $form_state, &$exclude) {
    if (!$form_state->isValueEmpty('op') && $form_state->getValue('op') == $this->options['reset_button_label']) {
      $this->resetForm($form$form_state);
    }
    if ($pager_plugin = $form_state->get('pager_plugin')) {
      $pager_plugin->exposedFormSubmit($form$form_state$exclude);
      $exclude[] = 'pager_plugin';
    }
  }

  /** * Resets all the states of the exposed form. * * This method is called when the "Reset" button is triggered. Clears * user inputs, stored session, and the form state. * * @param array $form * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */
parent::submitForm($form$form_state);
  }

  /** * {@inheritdoc} */
  public function save(array $form, FormStateInterface $form_state) {
    $account = $this->entity;
    $pass = $account->getPassword();
    $admin = $form_state->getValue('administer_users');
    $notify = !$form_state->isValueEmpty('notify');

    // Save has no return value so this cannot be tested.     // Assume save has gone through correctly.     $account->save();

    $form_state->set('user', $account);
    $form_state->setValue('uid', $account->id());

    $this->logger('user')->info('New user: %name %email.', ['%name' => $form_state->getValue('name'), '%email' => '<' . $form_state->getValue('mail') . '>', 'type' => $account->toLink($this->t('Edit'), 'edit-form')->toString()]);

    // Add plain text password into user account to generate mail tokens.

    elseif (!$was_defaulted && $is_defaulted) {
      // We used to have an override for this display, but the user now wants       // to go back to the default display.       // Overwrite the default display with the current form values, and make       // the current display use the new default values.       $display = &$this->getExecutable()->displayHandlers->get($display_id);
      // optionsOverride toggles the override of this section.       $display->setOverride($section);
    }

    if (!$form_state->isValueEmpty('name') && is_array($form_state->getValue('name'))) {
      // Loop through each of the items that were checked and add them to the view.       foreach (array_keys(array_filter($form_state->getValue('name'))) as $field) {
        [$table$field] = explode('.', $field, 2);

        if ($cut = strpos($field, '$')) {
          $field = substr($field, 0, $cut);
        }
        $id = $this->getExecutable()->addHandler($display_id$type$table$field);

        // check to see if we have group by settings         $key = $type;
        
'#default_value' => $this->configuration['random'],
      '#title' => $this->t('Randomize'),
      '#description' => $this->t('Randomize the rotation angle for each image. The angle specified above is used as a maximum.'),
    ];
    return $form;
  }

  /** * {@inheritdoc} */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    if (!$form_state->isValueEmpty('bgcolor') && !Color::validateHex($form_state->getValue('bgcolor'))) {
      $form_state->setErrorByName('bgcolor', $this->t('Background color must be a hexadecimal color value.'));
    }
  }

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

    $this->configuration['degrees'] = $form_state->getValue('degrees');
    
'#title' => $this->t('Allow Upscaling'),
      '#description' => $this->t('Let scale make images larger than their original size.'),
    ];
    return $form;
  }

  /** * {@inheritdoc} */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::validateConfigurationForm($form$form_state);
    if ($form_state->isValueEmpty('width') && $form_state->isValueEmpty('height')) {
      $form_state->setErrorByName('data', $this->t('Width and height can not both be blank.'));
    }
  }

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

    $this->configuration['upscale'] = $form_state->getValue('upscale');
  }
'#default_value' => $this->configuration['rankings'][$var] ?? 0,
      ];
    }
    return $form;
  }

  /** * {@inheritdoc} */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    foreach ($this->getRankings() as $var => $values) {
      if (!$form_state->isValueEmpty(['rankings', $var, 'value'])) {
        $this->configuration['rankings'][$var] = $form_state->getValue(['rankings', $var, 'value']);
      }
      else {
        unset($this->configuration['rankings'][$var]);
      }
    }
  }

  /** * {@inheritdoc} */
  
'offset' => $this->t('An offset from the current time such as "@example1" or "@example2"', ['@example1' => '+1 day', '@example2' => '-2 hours -30 minutes']),
        ],
        '#default_value' => !empty($this->value['type']) ? $this->value['type'] : 'date',
      ];
    }
    parent::valueForm($form$form_state);
  }

  public function validateOptionsForm(&$form, FormStateInterface $form_state) {
    parent::validateOptionsForm($form$form_state);

    if (!empty($this->options['exposed']) && $form_state->isValueEmpty(['options', 'expose', 'required'])) {
      // Who cares what the value is if it's exposed and non-required.       return;
    }

    $this->validateValidTime($form['value']$form_state$form_state->getValue(['options', 'operator'])$form_state->getValue(['options', 'value']));
  }

  public function validateExposed(&$form, FormStateInterface $form_state) {
    if (empty($this->options['exposed'])) {
      return;
    }

    

  public function testIsValueEmpty($key$expected) {
    $form_state = (new FormStateValuesTraitStub())->setValues([
      'foo' => 'one',
      'bar' => [
        'baz' => 'two',
      ],
      'true' => TRUE,
      'false' => FALSE,
      'null' => NULL,
    ]);
    $this->assertSame($expected$form_state->isValueEmpty($key));
  }

  /** * Provides data to self::testIsValueEmpty(). * * @return array[] * Items are arrays of two items: * - The key to check for in the form state (string) * - Whether the value is empty or not (bool). */
  public function providerIsValueEmpty() {
    
'#value' => $this->t('Submit'),
    ];
    return $form;
  }

  /** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    // Process the upload and perform validation. Note: we're using the     // form value for the $replace parameter.     if (!$form_state->isValueEmpty('file_subdir')) {
      $destination = 'temporary://' . $form_state->getValue('file_subdir');
      \Drupal::service('file_system')->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY);
    }
    else {
      $destination = FALSE;
    }

    // Preset custom error message if requested.     if ($form_state->getValue('error_message')) {
      $this->messenger->addError($form_state->getValue('error_message'));
    }

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