You are here

public function CouponGenerateForm::validateForm in Commerce Core 8.2

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

modules/promotion/src/Form/CouponGenerateForm.php, line 177

Class

CouponGenerateForm
Provides a form for bulk generating coupons.

Namespace

Drupal\commerce_promotion\Form

Code

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

  // Make sure that the total length doesn't exceed the database limit.
  $code_length = strlen($values['prefix']) + strlen($values['suffix']) + $values['length'];
  if ($code_length > self::MAX_CODE_LENGTH) {
    $form_state
      ->setError($form['pattern'], $this
      ->t('The total pattern length (@coupon_length) exceeds the maximum length allowed (@max_length).', [
      '@coupon_length' => $code_length,
      '@max_length' => self::MAX_CODE_LENGTH,
    ]));
  }

  // Validate that pattern for the given quantity.
  $quantity = $values['quantity'];
  $pattern = new CouponCodePattern($values['format'], $values['prefix'], $values['suffix'], $values['length']);
  if (!$this->couponCodeGenerator
    ->validatePattern($pattern, $quantity)) {
    $form_state
      ->setError($form['pattern'], $this
      ->t('This pattern cannot be used to generate @quantity coupons.', [
      '@quantity' => $quantity,
    ]));
  }
}