regex_field_validation.module in RegEx Field Validation 8
Same filename and directory in other branches
Contains regex_field_validation.module.
File
regex_field_validation.moduleView source
<?php
/**
* @file
* Contains regex_field_validation.module.
*/
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Config\Entity\ThirdPartySettingsInterface;
/**
* Implements hook_help().
*/
function regex_field_validation_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the regex_field_validation module.
case 'help.page.regex_field_validation':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('This module provides an extra validation option for text fields') . '</p>';
$output .= '<p>' . t('It uses the power of regular expressions to validate the content from fields') . '</p>';
$output .= '<h4>' . t('How to use it?') . '</h4>';
$output .= '<ul>';
$output .= '<li>' . t('Go to Manage Fields on any Content type') . '</li>';
$output .= '<li>' . t('Edit any text field settings') . '</li>';
$output .= '<li>' . t('Locate the RegEx Field Validation box that should be available in the settings form') . '</li>';
$output .= '<li>' . t('Check the Validate field with RegEx option to activate the Regular Expression and Error message fields') . '</li>';
$output .= '<li>' . t('Type in the regular expression that you want the field value to be matched against') . '</li>';
$output .= '<li>' . t('Type in the error message that you want to be displayed when the field value does not validate') . '</li>';
$output .= '</ul>';
$output .= '<h4>' . t('Examples of regular expressions that can be used') . '</h4>';
$output .= '<ul>';
$output .= '<li>' . '<code>' . '\\^\\d.\\d+\\Z\\' . '</code > - ' . '<b>' . t('Decimal followed by dot and unlimited decimals (1.12, 1.55, etc.') . '</b></li>';
$output .= '<li>' . '<code>' . '\\^[^<\\x09]{0,100}\\Z\\' . '</code> - ' . '<b>' . t('Text containing between 0 and 100 characters.') . '</b></li>';
$output .= '<li>' . '<code>' . '\\^[AaBb]\\Z\\' . '</code > - ' . '<b>' . t('1 character either A, a, B or b.') . '</b></li>';
$output .= '<li>' . '<code>' . '\\^(http|https):\\/\\/.{2,80}\\Z\\' . '</code> - ' . '<b>' . t('URL starting with “http” or “https” and contains between 2 and 80 characters.') . '</b></li>';
$output .= '<li>' . '<code>' . '\\^.{2,40}\\@.{2,50}\\..{2,5}\\Z\\' . '</code > - ' . '<b>' . t('Email address containing between 2 and 40 characters before the “@”, then between 2 and 50 characters as the domain name and between 2 and 5 as the top level domain') . '</b></li>';
$output .= '<li>' . '<code>' . '\\^(0[1-9]|1[0-2])\\Z\\' . '</code> - ' . '<b>' . t('2 digits that could represent the day of the month (01-31)') . '</b></li>';
$output .= '<li>' . '<code>' . '\\^(19|20|21)[0-9]{2}\\Z\\' . '</code > - ' . '<b>' . t('4 digits that could represent the year (1900-2199)') . '</b></li>';
$output .= '<li>' . '<code>' . '\\^(ABC|DEF|GHI|JKL|MNO|PQR|STU|VWX)?\\Z\\' . '</code> - ' . '<b>' . t('Accept a tree letter string that can be found in that list') . '</b></li>';
$output .= '<li>' . '<code>' . '\\^([0-9]+(\\.[0-9]{2})?)?\\Z\\' . '</code > - ' . '<b>' . t('Numeric with "." as decimal separator (29.99)') . '</b></li>';
$output .= '<li>' . '<code>' . '\\^[0-9.]{1,8}\\Z<\\' . '</code> - ' . '<b>' . t('Numerical value between 1 and 8 digits.') . '</b></li>';
$output .= '<li>' . '<code>' . '\\^[^<\\x09\\x0a\\x0d]{0,10}\\Z\\' . '</code > - ' . '<b>' . t('Single line between 0 and 10 characters that should not contain HTML markup') . '</b></li>';
$output .= '<li>' . '<code>' . '\\^[^<]{0,100}\\Z\\' . '</code> - ' . '<b>' . t('Multiple lines between 0 and 100 characters that should not contain HTML markup') . '</b></li>';
$output .= '<li>' . '<code>' . '\\^[^<\\x09\\x0a\\x0d]{0,1000}\\Z\\' . '</code > - ' . '<b>' . t('Text containing between 0 and 1000 letters, numbers and spaces') . '</b></li>';
$output .= '</ul>';
return $output;
default:
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function regex_field_validation_form_field_config_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$field = $form_state
->getBuildInfo()['callback_object']
->getEntity();
$field_types = [
'string',
'string_long',
];
if (in_array($field
->getType(), $field_types)) {
$field_state = [
'visible' => [
':input[name$="[regex_field_validation][enable]"]' => [
'checked' => TRUE,
],
],
];
$form['third_party_settings']['regex_field_validation'] = [
'#type' => 'fieldset',
'#title' => t('RegEx Field Validation'),
'#collapsible' => TRUE,
'#collapsed' => $field_state,
];
$form['third_party_settings']['regex_field_validation']['enable'] = [
'#type' => 'checkbox',
'#title' => t('Validate field with RegEx'),
'#description' => t('Validate field content using regular expression.'),
'#default_value' => $field
->getThirdPartySetting('regex_field_validation', 'enable'),
];
$form['third_party_settings']['regex_field_validation']['regex'] = [
'#type' => 'textarea',
'#title' => t('Regular Expression'),
'#states' => $field_state,
'#default_value' => $field
->getThirdPartySetting('regex_field_validation', 'regex'),
'#description' => t('Regular expression used to validate the field'),
];
$form['third_party_settings']['regex_field_validation']['error_message'] = [
'#type' => 'textfield',
'#title' => t('Error message'),
'#states' => $field_state,
'#default_value' => $field
->getThirdPartySetting('regex_field_validation', 'error_message'),
'#description' => t('The error message that will be displayed if the field does not validate'),
];
}
}
/**
* Implements hook_entity_bundle_field_info_alter().
*/
function regex_field_validation_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) {
if ($entity_type
->id() == 'node') {
foreach ($fields as $field) {
if ($field instanceof ThirdPartySettingsInterface) {
$settings = $field
->getThirdPartySettings('regex_field_validation');
if (!empty($settings)) {
if ($settings['enable'] == 1) {
$fields[$field
->getName()]
->addConstraint('RegExValidationConstraint', [
'regex' => $settings['regex'],
'errorMessage' => $settings['error_message'],
]);
}
}
}
}
}
}
Functions
Name | Description |
---|---|
regex_field_validation_entity_bundle_field_info_alter | Implements hook_entity_bundle_field_info_alter(). |
regex_field_validation_form_field_config_edit_form_alter | Implements hook_form_FORM_ID_alter(). |
regex_field_validation_help | Implements hook_help(). |