Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x

Country.php

Namespace

Symfony\Component\Validator\Constraints

File

vendor/symfony/validator/Constraints/Country.php

View source
<?php


/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Intl\Countries;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\LogicException;

/**
 * Validates a value is a valid ISO 3166-1 alpha-2 country code.
 *
 * @see https://en.wikipedia.org/wiki/ISO_3166-1#Current_codes
 *
 * @author Bernhard Schussek <bschussek@gmail.com>
 */
class Country extends Constraint {
    public const NO_SUCH_COUNTRY_ERROR = '8f900c12-61bd-455d-9398-996cd040f7f0';
    protected const ERROR_NAMES = [
        self::NO_SUCH_COUNTRY_ERROR => 'NO_SUCH_COUNTRY_ERROR',
    ];
    public string $message = 'This value is not a valid country.';
    public bool $alpha3 = false;
    
    /**
     * @param array<string,mixed>|null $options
     * @param bool|null                $alpha3  Whether to check for alpha-3 codes instead of alpha-2 (defaults to false)
     * @param string[]|null            $groups
     *
     * @see https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3#Current_codes
     */
    public function __construct(?array $options = null, ?string $message = null, ?bool $alpha3 = null, ?array $groups = null, mixed $payload = null) {
        if (!class_exists(Countries::class)) {
            throw new LogicException('The Intl component is required to use the Country constraint. Try running "composer require symfony/intl".');
        }
        parent::__construct($options, $groups, $payload);
        $this->message = $message ?? $this->message;
        $this->alpha3 = $alpha3 ?? $this->alpha3;
    }

}

Classes

Title Deprecated Summary
Country Validates a value is a valid ISO 3166-1 alpha-2 country code.
RSS feed
Powered by Drupal