You are here

public function NewPackageForm::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

shipping/uc_fulfillment/src/Form/NewPackageForm.php, line 136

Class

NewPackageForm
Puts ordered products into a package.

Namespace

Drupal\uc_fulfillment\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  if ($this
    ->t('Cancel') != (string) $form_state
    ->getValue('op')) {

    // See if any products have been checked.
    foreach ($form_state
      ->getValue('shipping_types') as $shipping_type => $products) {
      foreach ($products['table'] as $id => $product) {
        if ($product['checked']) {

          // At least one has been checked, that's all we need.
          return;
        }
      }
    }

    // If nothing is checked, set error.
    $form_state
      ->setErrorByName($shipping_type, $this
      ->t('Packages must contain at least one product.'));
  }
}