You are here

public function RoleFeatureForm::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_role/src/Form/RoleFeatureForm.php, line 237

Class

RoleFeatureForm
Creates or edits a role feature for a product.

Namespace

Drupal\uc_role\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {

  // Invalid quantity?
  if ($form_state
    ->getValue('expiration') === 'abs') {
    $this
      ->messenger()
      ->addMessage(var_export($form_state
      ->getValue('expire_absolute'), TRUE));
    if ($form_state
      ->getValue('expire_absolute')
      ->getTimestamp() <= $this->time
      ->getRequestTime()) {
      $form_state
        ->setErrorByName('expire_absolute', $this
        ->t('The specified date @date has already occurred. Please choose another.', [
        '@date' => $this->dateFormatter
          ->format($form_state
          ->getValue('expire_absolute')
          ->getTimestamp()),
      ]));
    }
  }
  else {
    if ($form_state
      ->getValue('expire_relative_granularity') != 'never' && intval($form_state
      ->getValue('expire_relative_duration')) < 1) {
      $form_state
        ->setErrorByName('expire_relative_duration', $this
        ->t('The amount of time must be a positive integer.'));
    }
  }

  // No roles?
  if ($form_state
    ->isValueEmpty('role')) {
    $form_state
      ->setErrorByName('role', $this
      ->t('You must have a role to assign. You may need to <a href=":role_url">create a new role</a> or perhaps <a href=":feature_url">set role assignment defaults</a>.', [
      ':role_url' => Url::fromRoute('user.role_add')
        ->toString(),
      ':feature_url' => Url::fromRoute('uc_product.settings')
        ->toString(),
    ]));
  }

  // This role already set on this SKU?
  if (!$form_state
    ->hasValue('pfid') && ($product_roles = \Drupal::database()
    ->query('SELECT * FROM {uc_roles_products} WHERE nid = :nid AND model = :model AND rid = :rid', [
    ':nid' => $form_state
      ->getValue('nid'),
    ':model' => $form_state
      ->getValue('model'),
    ':rid' => $form_state
      ->getValue('role'),
  ])
    ->fetchObject())) {
    $form_state
      ->setErrorByName('role', $this
      ->t('The combination of SKU and role already exists for this product.'));
    $form_state
      ->setErrorByName('model');
  }
}