CountryCodeConstraint.php

Namespace

Drupal\Core\Validation\Plugin\Validation\Constraint

File

core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/CountryCodeConstraint.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Core\Validation\Plugin\Validation\Constraint;

use Drupal\Core\Locale\CountryManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Validation\Attribute\Constraint;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraints\Choice;
class CountryCodeConstraint implements ContainerFactoryPluginInterface {
  
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) : Choice {
    $countries = $container->get(CountryManagerInterface::class)
      ->getList();
    $configuration['choices'] = array_keys($countries);
    return new Choice($configuration);
  }

}

Classes

Title Deprecated Summary
CountryCodeConstraint #[Constraint(id: 'CountryCode', label: new TranslatableMarkup('CountryCode', [], [ 'context' => 'Validation', ]))]

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.