You are here

public function CurrencyForm::validateNumericCode in Price 3.x

Same name and namespace in other branches
  1. 8 src/Form/CurrencyForm.php \Drupal\price\Form\CurrencyForm::validateNumericCode()
  2. 2.0.x src/Form/CurrencyForm.php \Drupal\price\Form\CurrencyForm::validateNumericCode()
  3. 2.x src/Form/CurrencyForm.php \Drupal\price\Form\CurrencyForm::validateNumericCode()
  4. 3.0.x src/Form/CurrencyForm.php \Drupal\price\Form\CurrencyForm::validateNumericCode()

Validates the numeric code.

File

src/Form/CurrencyForm.php, line 120

Class

CurrencyForm

Namespace

Drupal\price\Form

Code

public function validateNumericCode(array $element, FormStateInterface $form_state, array $form) {
  $currency = $this
    ->getEntity();
  $numeric_code = $element['#value'];
  if ($numeric_code && !preg_match('/^\\d{3}$/i', $numeric_code)) {
    $form_state
      ->setError($element, $this
      ->t('The numeric code must consist of three digits.'));
  }
  elseif ($currency
    ->isNew()) {
    $loaded_currencies = $this->storage
      ->loadByProperties([
      'numericCode' => $numeric_code,
    ]);
    if ($loaded_currencies) {
      $form_state
        ->setError($element, $this
        ->t('The numeric code is already in use.'));
    }
  }
}