You are here

class PhoneFieldValidationRule in Field Validation 8

PhoneFieldValidationRule.

Plugin annotation


@FieldValidationRule(
  id = "phone_field_validation_rule",
  label = @Translation("Phone"),
  description = @Translation("Verifies that user-entered values are phone number.")
)

Hierarchy

Expanded class hierarchy of PhoneFieldValidationRule

File

src/Plugin/FieldValidationRule/PhoneFieldValidationRule.php, line 18

Namespace

Drupal\field_validation\Plugin\FieldValidationRule
View source
class PhoneFieldValidationRule extends ConfigurableFieldValidationRuleBase {

  /**
   * {@inheritdoc}
   */
  public function addFieldValidationRule(FieldValidationRuleSetInterface $field_validation_rule_set) {
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function getSummary() {
    $summary = parent::getSummary();
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'country' => NULL,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $countries = $this
      ->phone_countries();
    $country_options = array();
    foreach ($countries as $country_code => $country) {
      $country_options[$country_code] = isset($country['name']) ? $country['name'] : '';
    }
    $form['country'] = [
      '#title' => $this
        ->t('Country'),
      '#type' => 'select',
      '#options' => $country_options,
      '#default_value' => $this->configuration['country'],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form, $form_state);
    $this->configuration['country'] = $form_state
      ->getValue('country');
  }
  public function validate($params) {
    $value = isset($params['value']) ? $params['value'] : '';
    $rule = isset($params['rule']) ? $params['rule'] : null;
    $context = isset($params['context']) ? $params['context'] : null;
    $settings = array();
    if (!empty($rule) && !empty($rule->configuration)) {
      $settings = $rule->configuration;
    }
    if ($value !== '' && !is_null($value)) {
      $country_code = isset($settings['country']) ? $settings['country'] : '';
      $country_regex = '';
      $countries = $this
        ->phone_countries();
      $country_regex = isset($countries[$country_code]['regex']) ? $countries[$country_code]['regex'] : '';
      if (!preg_match($country_regex, $value)) {
        $context
          ->addViolation($rule
          ->getErrorMessage());
      }
    }
  }
  public function phone_countries() {
    $countries = [
      'fr' => [
        'name' => $this
          ->t('France'),
        'regex' => '/(\\+33|0)([1-9]\\d{8}|85\\d{7}|87[0-57-9]\\d{6})$/',
      ],
      'be' => [
        'name' => $this
          ->t('Belgium'),
        'regex' => '/^(\\+32|0)[1-9]\\d{7,8}$/i',
      ],
      'it' => [
        'name' => $this
          ->t('Italy'),
        'regex' => "/^(\\+39)?[ ]?([0-9]{2,3}(\\/|-| )?[0-9]{6,7})\$/i",
      ],
      'el' => [
        'name' => $this
          ->t('Greece'),
        'regex' => "/^(\\+30)?[ ]?([0-9]{3,4}(\\/|-| )?[0-9]{6,7})\$/i",
      ],
      'ch' => [
        'name' => $this
          ->t('Switzerland'),
        'regex' => "%(\\+41|0|0041)([2-9]\\d{8})\$%",
      ],
      'ca' => [
        'name' => $this
          ->t('US & Canada'),
        'regex' => '/\\D*(\\d*)\\D*[2-9][0-8]\\d\\D*[2-9]\\d{2}\\D*\\d{4}\\D*\\d*\\D*/x',
      ],
      'cr' => [
        'name' => $this
          ->t('Costa Rica'),
        'regex' => "/(00)?[\\s|-]?((\\+)?[\\s|-]?[0-9]{3})?[\\s|-]?([0-9]{2})[\\s|-]?([0-9]{2})[\\s|-]?([0-9]{2})[\\s|-]?([0-9]{2})[\\s|-]?/",
      ],
      'pa' => [
        'name' => $this
          ->t('Panama'),
        'regex' => '/((00|\\+)?[0-9]{3}[\\s])?([0-9]{3,4})[\\s|-]?([0-9]{4})/',
      ],
      'gb' => [
        'name' => $this
          ->t('Great Britain - United Kingdom'),
        'regex' => "/((^\\+44\\s?(\\(0\\))?\\d{4}|^\\(?0\\d{4}\\)?){1}\\s?\\d{3}\\s?\\d{3}|(^\\+44\\s?(\\(0\\))?\\d{3}|^\\(?0\\d{3}\\)?){1}\\s?\\d{3}\\s?\\d{4}|(^\\+44\\s?(\\(0\\))?\\d{2}|^\\(?0\\d{2}\\)?){1}\\s?\\d{4}\\s?\\d{4}|(^\\+44\\s?(\\(0\\))?\\d{1}|^\\(?0\\d{1}\\)?){1}\\s?\\d{5}\\s?\\d{5})(\\s?\\#\\d*)?/x",
      ],
      'ru' => [
        'name' => $this
          ->t('Russia'),
        'regex' => "/^\\D*[78]?\\D*\\d{3,5}\\D*\\d{1,3}\\D*\\d{2}\\D*\\d{2}\\D*/x",
      ],
      /*
            'ua' => [
              'name' => t('Ukraine'),
              'regex' => '',
            ], */
      'es' => [
        'name' => $this
          ->t('Spain'),
        'regex' => '/^[0-9]{2,3}-? ?[0-9]{6,7}$/',
      ],
      /*
            'au' => [
              'name' => t('Australia'),
              'regex' => '',
            ], */
      'cs' => [
        'name' => $this
          ->t('Czech Republic'),
        'regex' => '/^((?:\\+|00)420)? ?(\\d{3}) ?(\\d{3}) ?(\\d{3})$/',
      ],
      'hu' => [
        'name' => $this
          ->t('Poland - mobiles only'),
        'regex' => "/^\\D*(?:\\+?36|06)?(\\d\\d?)\\D*(\\d{3})\\D*(\\d{3,4})\\D*\$/x",
      ],
      'pl' => [
        'name' => $this
          ->t('Poland'),
        'regex' => "/^(\\+48\\s+)?\\d{3}(\\s*|\\-)\\d{3}(\\s*|\\-)\\d{3}\$/i",
      ],
      'nl' => [
        'name' => $this
          ->t('Netherland'),
        'regex' => '/([0]{1}[6]{1}[-\\s]+[1-9]{1}[\\s]*([0-9]{1}[\\s]*){7})|([0]{1}[1-9]{1}[0-9]{2}[-\\s]+[1-9]{1}[\\s]*([0-9]{1}[\\s]*){5})|([0]{1}[1-9]{1}[0-9]{1}[-\\s]+[1-9]{1}[\\s]*([0-9]{1}[\\s]*){6})/x',
      ],
      'se' => [
        'name' => $this
          ->t('Sweden'),
        'regex' => "/^(([+]\\d{2}[ ][1-9]\\d{0,2}[ ])|([0]\\d{1,3}[-]))((\\d{2}([ ]\\d{2}){2})|(\\d{3}([ ]\\d{3})*([ ]\\d{2})+))\$/i",
      ],
      'za' => [
        'name' => $this
          ->t('South Africa'),
        'regex' => '/^((?:\\+27|27)|0)[ ]*((\\d{2})(-| )?(\\d{3})(-| )?(\\d{4})|(\\d{2})( |-)(\\d{7}))$/',
      ],
      /*
            'il' => [
              'name' => t('Israel'),
              'regex' => '',
            ], */

      /*
            'nz' => [
              'name' => t('New Zealand'),
              'regex' => '',
            ], */
      'br' => [
        'name' => $this
          ->t('Brazil'),
        'regex' => "/^(\\+|0{2}|)?(55|0|)[ -.]?((\\(0?[1-9][0-9]\\))|(0?[1-9][0-9]))[ -.]?([1-9][0-9]{2,3})[ -.]?([0-9]{4})\$/",
      ],
      'cl' => [
        'name' => $this
          ->t('Chile'),
        'regex' => "/^((\\(\\d{3}\\) ?)|(\\d{3}-)|(\\(\\d{2}\\) ?)|(\\d{2}-)|(\\(\\d{1}\\) ?)|(\\d{1}-))?\\d{3}-(\\d{3}|\\d{4})\$/i",
      ],
      'cn' => [
        'name' => $this
          ->t('China'),
        'regex' => '/^(\\+86|86)?( |-)?([0-9]{11}|([0-9]{3,4}(\\-|\\.| )[0-9]{3,8})|[0-9]{2}( |\\-)[0-9]{4}[ ][0-9]{4}|[0-9]{2}\\.[0-9]{2}\\.[0-9]{2}\\.[0-9]{2}\\.[0-9]{2})$/',
      ],
      /*
            'hk' => [
              'name' => t('Hong-Kong'),
              'regex' => '',
            ],
            'mo' => [
              'name' => t('Macao'),
              'regex' => '',
            ],  */
      'ph' => [
        'name' => $this
          ->t('The Philippines'),
        'regex' => "/((^\\+63\\s?\\(?\\d{5}\\)?|^\\(?\\d{5}\\)?){1}\\s?\\d{3}(\\S?|\\s?)?\\d{4}|(^\\+63\\s?\\(?\\d{4}\\)?|^\\(?\\d{4}\\)?){1}\\s?\\d{3}(\\S?|\\s?)?\\d{4}|(^\\+63\\s?\\(?\\d{3}\\)?|^\\(?\\d{3}\\)?){1}\\s?\\d{3}(\\S?|\\s?)?\\d{4}|(^\\+63\\s?\\(?\\d{2}\\)?|^\\(?\\d{2}\\)?){1}\\s?\\d{3}(\\S?|\\s?)?\\d{4}|(^\\+63\\s?\\(?\\d{1}\\)?|^\\(?\\d{1}\\)?){1}\\s?\\d{3}(\\S?|\\s?)?\\d{4})(\\s?\\#\\d*)?/x",
      ],
      'sg' => [
        'name' => $this
          ->t('Singapore'),
        'regex' => '/^(\\+65)?\\s?[689]\\d{7}$/i',
      ],
      'jo' => [
        'name' => $this
          ->t('Jordan'),
        'regex' => "/(^(\\+962|00962|962|0)[-\\s]{0,1}[7]{1}[7-9]{1}[0-9]{7}\$) | (^(\\+962|00962|962|0)[-\\s]{0,1}[2-6][-\\s]{0,1}[0-9]{7}\$)/x",
      ],
      /*
            'eg' => [
              'name' => t('Egypt'),
              'regex' => '',
            ], */
      'pk' => [
        'name' => $this
          ->t('Pakistan'),
        'regex' => "/^(\\+)?([9]{1}[2]{1})?-? ?(\\()?([0]{1})?[1-9]{2,4}(\\))?-? ??(\\()?[1-9]{4,7}(\\))?\$/i",
      ],
      'in' => [
        'name' => $this
          ->t('India'),
        'regex' => "/^((\\+*)((0[ -]+)*|(91 )*)(\\d{12}+|\\d{10}+))|\\d{5}([- ]*)\\d{6}\$/i",
      ],
      'dk' => [
        'name' => $this
          ->t('Denmark'),
        'regex' => "/^(([+]\\d{2}[ ][1-9]\\d{0,2}[ ])|([0]\\d{1,3}[-]))((\\d{2}([ ]\\d{2}){2})|(\\d{3}([ ]\\d{3})*([ ]\\d{2})+))\$/i",
      ],
    ];
    return $countries;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigurableFieldValidationRuleBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FieldValidationRuleBase::$column protected property The column of the FieldValidationRule.
FieldValidationRuleBase::$error_message protected property The error message of the FieldValidationRule.
FieldValidationRuleBase::$field_name protected property The field name of the FieldValidationRule.
FieldValidationRuleBase::$logger protected property A logger instance.
FieldValidationRuleBase::$title protected property The title of the FieldValidationRule.
FieldValidationRuleBase::$uuid protected property The FieldValidationRule ID.
FieldValidationRuleBase::$weight protected property The weight of the FieldValidationRule.
FieldValidationRuleBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
FieldValidationRuleBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
FieldValidationRuleBase::getColumn public function Returns the column of the field_validation_rule. Overrides FieldValidationRuleInterface::getColumn
FieldValidationRuleBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
FieldValidationRuleBase::getDerivativeExtension public function Returns the extension the derivative would have have after adding this field_validation_rule. Overrides FieldValidationRuleInterface::getDerivativeExtension
FieldValidationRuleBase::getErrorMessage public function Returns the error message of the field_validation_rule. Overrides FieldValidationRuleInterface::getErrorMessage
FieldValidationRuleBase::getFieldName public function Returns the field name of the field_validation_rule. Overrides FieldValidationRuleInterface::getFieldName
FieldValidationRuleBase::getTitle public function Returns the title of the field_validation_rule. Overrides FieldValidationRuleInterface::getTitle
FieldValidationRuleBase::getUuid public function Returns the unique ID representing the field_validation_rule. Overrides FieldValidationRuleInterface::getUuid
FieldValidationRuleBase::getWeight public function Returns the weight of the field_validation_rule. Overrides FieldValidationRuleInterface::getWeight
FieldValidationRuleBase::label public function Returns the field_validation_rule label. Overrides FieldValidationRuleInterface::label
FieldValidationRuleBase::setColumn public function Sets the column for this field_validation_rule. Overrides FieldValidationRuleInterface::setColumn
FieldValidationRuleBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
FieldValidationRuleBase::setErrorMessage public function Sets the error message for this field_validation_rule. Overrides FieldValidationRuleInterface::setErrorMessage
FieldValidationRuleBase::setFieldName public function Sets the field name for this field_validation_rule. Overrides FieldValidationRuleInterface::setFieldName
FieldValidationRuleBase::setTitle public function Sets the title for this field_validation_rule. Overrides FieldValidationRuleInterface::setTitle
FieldValidationRuleBase::setWeight public function Sets the weight for this field_validation_rule. Overrides FieldValidationRuleInterface::setWeight
FieldValidationRuleBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PhoneFieldValidationRule::addFieldValidationRule public function Applies a field_validation_rule to the field_validation_rule_set. Overrides FieldValidationRuleInterface::addFieldValidationRule
PhoneFieldValidationRule::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
PhoneFieldValidationRule::defaultConfiguration public function Gets default configuration for this plugin. Overrides FieldValidationRuleBase::defaultConfiguration
PhoneFieldValidationRule::getSummary public function Returns a render array summarizing the configuration of the field_validation_rule. Overrides FieldValidationRuleBase::getSummary
PhoneFieldValidationRule::phone_countries public function
PhoneFieldValidationRule::submitConfigurationForm public function Form submission handler. Overrides ConfigurableFieldValidationRuleBase::submitConfigurationForm
PhoneFieldValidationRule::validate public function Overrides FieldValidationRuleBase::validate
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.