You are here

public function ObjectOptionsFormBase::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_attribute/src/Form/ObjectOptionsFormBase.php, line 180

Class

ObjectOptionsFormBase
Defines the class/product attributes options form.

Namespace

Drupal\uc_attribute\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $error = FALSE;
  foreach ($form_state
    ->getValue('attributes') as $attribute) {
    $selected_opts = [];
    if (isset($attribute['options'])) {
      foreach ($attribute['options'] as $oid => $option) {
        if ($option['select']) {
          $selected_opts[] = $oid;
        }
      }
    }
    if (!empty($selected_opts) && !in_array($attribute['default'], $selected_opts)) {
      $form_state
        ->setErrorByName($attribute['default']);
      $error = TRUE;
    }
  }
  if ($error) {
    $this
      ->messenger()
      ->addError($this
      ->t('All attributes with enabled options must specify an enabled option as default.'));
  }
}