You are here

public function CurrencyForm::validateCurrencyCode in Currency 8.3

Implements #element_validate for the currency code element.

File

src/Entity/Currency/CurrencyForm.php, line 171

Class

CurrencyForm
Provides a currency add/edit form.

Namespace

Drupal\currency\Entity\Currency

Code

public function validateCurrencyCode(array $element, FormStateInterface $form_state, array $form) {
  $currency = $this
    ->getEntity();
  $currency_code = $element['#value'];
  if (!preg_match('/^[a-z]{3}$/i', $currency_code)) {
    $form_state
      ->setError($element, $this
      ->t('The currency code must be three letters.'));
  }
  elseif ($currency
    ->isNew()) {
    $loaded_currency = $this->currencyStorage
      ->load($currency_code);
    if ($loaded_currency) {
      $form_state
        ->setError($element, $this
        ->t('The currency code is already in use by @link.', array(
        '@link' => $this->linkGenerator
          ->generate($loaded_currency
          ->label(), $loaded_currency
          ->toUrl('edit-form')),
      )));
    }
  }
}