You are here

public function CurrencyForm::validateCurrencyCode in Price 8

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

Validates the currency code.

File

src/Form/CurrencyForm.php, line 103

Class

CurrencyForm

Namespace

Drupal\price\Form

Code

public function validateCurrencyCode(array $element, FormStateInterface $form_state, array $form) {
  $currency = $this
    ->getEntity();
  $currency_code = $element['#value'];
  if (!preg_match('/^[A-Z]{3}$/', $currency_code)) {
    $form_state
      ->setError($element, $this
      ->t('The currency code must consist of three uppercase letters.'));
  }
  elseif ($currency
    ->isNew()) {
    $loaded_currency = $this->storage
      ->load($currency_code);
    if ($loaded_currency) {
      $form_state
        ->setError($element, $this
        ->t('The currency code is already in use.'));
    }
  }
}