You are here

function uc_role_user_validate in Ubercart 8.4

User profile form validate handler.

See also

uc_role_form_user_profile_form_alter()

1 string reference to 'uc_role_user_validate'
uc_role_form_user_profile_form_alter in uc_role/uc_role.module
Implements hook_form_user_profile_form_alter().

File

uc_role/uc_role.module, line 221
Grants roles upon accepted payment of products.

Code

function uc_role_user_validate($form, FormStateInterface $form_state) {
  $edit = $form_state
    ->getValues();

  // Validate the amount of time for the expiration.
  if (!empty($edit['new_role'])) {
    if (intval($edit['new_role_add_qty']) < 1) {
      $form_state
        ->setErrorByName('new_role_add_qty', t('The expiration length must be a positive integer'));
    }
  }

  // Validate adjusted expirations.
  if (isset($edit['table'])) {
    foreach ((array) $edit['table'] as $rid => $value) {

      // We don't validate if nothing was actually selected, the role, or the
      // expiration is removed.
      if ($value['qty'] == 0 || $value['remove'] == 1 || !$edit['roles'][$rid]) {
        continue;
      }
      $qty = $value['qty'];
      $qty *= $value['polarity'] == 'add' ? 1 : -1;
      $new_expiration = _uc_role_get_expiration($qty, $value['granularity'], $value['expiration']);
      if (\Drupal::time()
        ->getRequestTime() > $new_expiration) {
        $form_state
          ->setErrorByName('qty', t('The new expiration date, %date, has already occurred.', [
          '%date' => \Drupal::service('date.formatter')
            ->format($new_expiration, 'short'),
        ]));
      }
    }
  }
}