You are here

public function TwitterWidgetForm::validateForm in Twitter Profile Widget 8

Button-level validation handlers are highly discouraged for entity forms, as they will prevent entity validation from running. If the entity is going to be saved during the form submission, this method should be manually invoked from the button-level validation handler, otherwise an exception will be thrown.

Overrides ContentEntityForm::validateForm

File

src/Form/TwitterWidgetForm.php, line 99

Class

TwitterWidgetForm
Form controller for Twitter widget edit forms.

Namespace

Drupal\twitter_profile_widget\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);
  $entity = $this
    ->buildEntity($form, $form_state);
  $account = $entity
    ->get('account')->value;
  $type = $entity
    ->get('type')->value;
  $search = $entity
    ->get('search')->value;
  $timeline = $entity
    ->get('timeline')->value;
  $count = $entity
    ->get('count')->value;
  $replies = $entity
    ->get('replies')->value;
  $retweets = $entity
    ->get('retweets')->value;
  if ($type == 'search' && $search == '') {
    $form_state
      ->setErrorByName('search', $this
      ->t('The "Search term" type requires entering a search parameter.'));
  }
  if ($type !== 'search' && $account == '') {
    $form_state
      ->setErrorByName('account', $this
      ->t('This Twitter widget type requires that you enter an account handle.'));
  }
  if ($type == 'timeline' && $timeline == '') {
    $form_state
      ->setErrorByName('timeline', $this
      ->t('The "User timeline" type requires entering a timeline list.'));
  }
  return $entity;
}