You are here

public function UserForm::validateForm in Ubercart 8.4

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormBase::validateForm

File

uc_file/src/Form/UserForm.php, line 205

Class

UserForm
Creates or edits a file feature for a product.

Namespace

Drupal\uc_file\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $edit = $form_state
    ->getValues();

  // Determine if any downloads were modified.
  if (isset($edit['file_download'])) {
    foreach ((array) $edit['file_download'] as $key => $download_modification) {

      // We don't care... it's about to be deleted.
      if ($download_modification['remove']) {
        continue;
      }
      if ($download_modification['download_limit'] < 0) {
        $form_state
          ->setErrorByName('file_download][' . $key . '][download_limit', $this
          ->t('A negative download limit does not make sense. Please enter a positive integer, or leave empty for no limit.'));
      }
      if ($download_modification['address_limit'] < 0) {
        $form_state
          ->setErrorByName('file_download][' . $key . '][address_limit', $this
          ->t('A negative address limit does not make sense. Please enter a positive integer, or leave empty for no limit.'));
      }

      // Some expirations don't need any validation...
      if ($download_modification['time_granularity'] == 'never' || !$download_modification['time_quantity']) {
        continue;
      }

      // Either use the current expiration, or if there's none,
      // start from right now.
      $new_expiration = _uc_file_expiration_date($download_modification, $download_modification['expiration']);
      if ($new_expiration <= $this->time
        ->getRequestTime()) {
        $form_state
          ->setErrorByName('file_download][' . $key . '][time_quantity', $this
          ->t('The date %date has already occurred.', [
          '%date' => $this->dateFormatter
            ->format($new_expiration, 'short'),
        ]));
      }
      if ($download_modification['time_quantity'] < 0) {
        $form_state
          ->setErrorByName('file_download][' . $key . '][time_quantity', $this
          ->t('A negative expiration quantity does not make sense. Use the polarity control to determine if the time should be added or subtracted.'));
      }
    }
  }
}