You are here

public function CurrencyForm::validateCurrencyNumber in Currency 8.3

Implements #element_validate for the currency number element.

File

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

Class

CurrencyForm
Provides a currency add/edit form.

Namespace

Drupal\currency\Entity\Currency

Code

public function validateCurrencyNumber(array $element, FormStateInterface $form_state, array $form) {
  $currency = $this
    ->getEntity();
  $currency_number = $element['#value'];
  if ($currency_number && !preg_match('/^\\d{3}$/i', $currency_number)) {
    $form_state
      ->setError($element, $this
      ->t('The currency number must be three digits.'));
  }
  elseif ($currency
    ->isNew()) {
    $loaded_currencies = $this->currencyStorage
      ->loadByProperties(array(
      'currencyNumber' => $currency_number,
    ));
    if ($loaded_currencies) {
      $loaded_currency = reset($loaded_currencies);
      $form_state
        ->setError($element, $this
        ->t('The currency number is already in use by @link.', array(
        '@link' => $this->linkGenerator
          ->generate($loaded_currency
          ->label(), $loaded_currency
          ->toUrl('edit-form')),
      )));
    }
  }
}