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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\field_validation\FieldValidationRuleBase implements ContainerFactoryPluginInterface, FieldValidationRuleInterface
- class \Drupal\field_validation\ConfigurableFieldValidationRuleBase implements ConfigurableFieldValidationRuleInterface
- class \Drupal\field_validation\Plugin\FieldValidationRule\PhoneFieldValidationRule
- class \Drupal\field_validation\ConfigurableFieldValidationRuleBase implements ConfigurableFieldValidationRuleInterface
- class \Drupal\field_validation\FieldValidationRuleBase implements ContainerFactoryPluginInterface, FieldValidationRuleInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of PhoneFieldValidationRule
File
- src/
Plugin/ FieldValidationRule/ PhoneFieldValidationRule.php, line 18
Namespace
Drupal\field_validation\Plugin\FieldValidationRuleView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigurableFieldValidationRuleBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FieldValidationRuleBase:: |
protected | property | The column of the FieldValidationRule. | |
FieldValidationRuleBase:: |
protected | property | The error message of the FieldValidationRule. | |
FieldValidationRuleBase:: |
protected | property | The field name of the FieldValidationRule. | |
FieldValidationRuleBase:: |
protected | property | A logger instance. | |
FieldValidationRuleBase:: |
protected | property | The title of the FieldValidationRule. | |
FieldValidationRuleBase:: |
protected | property | The FieldValidationRule ID. | |
FieldValidationRuleBase:: |
protected | property | The weight of the FieldValidationRule. | |
FieldValidationRuleBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
FieldValidationRuleBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
FieldValidationRuleBase:: |
public | function |
Returns the column of the field_validation_rule. Overrides FieldValidationRuleInterface:: |
|
FieldValidationRuleBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
FieldValidationRuleBase:: |
public | function |
Returns the extension the derivative would have have after adding this
field_validation_rule. Overrides FieldValidationRuleInterface:: |
|
FieldValidationRuleBase:: |
public | function |
Returns the error message of the field_validation_rule. Overrides FieldValidationRuleInterface:: |
|
FieldValidationRuleBase:: |
public | function |
Returns the field name of the field_validation_rule. Overrides FieldValidationRuleInterface:: |
|
FieldValidationRuleBase:: |
public | function |
Returns the title of the field_validation_rule. Overrides FieldValidationRuleInterface:: |
|
FieldValidationRuleBase:: |
public | function |
Returns the unique ID representing the field_validation_rule. Overrides FieldValidationRuleInterface:: |
|
FieldValidationRuleBase:: |
public | function |
Returns the weight of the field_validation_rule. Overrides FieldValidationRuleInterface:: |
|
FieldValidationRuleBase:: |
public | function |
Returns the field_validation_rule label. Overrides FieldValidationRuleInterface:: |
|
FieldValidationRuleBase:: |
public | function |
Sets the column for this field_validation_rule. Overrides FieldValidationRuleInterface:: |
|
FieldValidationRuleBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
FieldValidationRuleBase:: |
public | function |
Sets the error message for this field_validation_rule. Overrides FieldValidationRuleInterface:: |
|
FieldValidationRuleBase:: |
public | function |
Sets the field name for this field_validation_rule. Overrides FieldValidationRuleInterface:: |
|
FieldValidationRuleBase:: |
public | function |
Sets the title for this field_validation_rule. Overrides FieldValidationRuleInterface:: |
|
FieldValidationRuleBase:: |
public | function |
Sets the weight for this field_validation_rule. Overrides FieldValidationRuleInterface:: |
|
FieldValidationRuleBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PhoneFieldValidationRule:: |
public | function |
Applies a field_validation_rule to the field_validation_rule_set. Overrides FieldValidationRuleInterface:: |
|
PhoneFieldValidationRule:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
|
PhoneFieldValidationRule:: |
public | function |
Gets default configuration for this plugin. Overrides FieldValidationRuleBase:: |
|
PhoneFieldValidationRule:: |
public | function |
Returns a render array summarizing the configuration of the field_validation_rule. Overrides FieldValidationRuleBase:: |
|
PhoneFieldValidationRule:: |
public | function | ||
PhoneFieldValidationRule:: |
public | function |
Form submission handler. Overrides ConfigurableFieldValidationRuleBase:: |
|
PhoneFieldValidationRule:: |
public | function |
Overrides FieldValidationRuleBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |