Skip to content

Repository files navigation

Form API Validation

This module extends the form API to include convenient access to common for submission filters and validation checks. The core form API has no built in validators available to you, nor filters, and all have to be manually placed in a validation function. With this module you simply add the basic filers and/or validators you want to have in your form render area.

You can use the existent filters and rules or create your own. This module is fully extensible using hooks, allowing you to easily add your own and reuse them throughout the site. Abstraction at it's finest!

Available Validators

RuleUsageDescription
numericnumericMust contains only numbers.
alphaalphaMust contains only alpha characters.
lengthlength[<total>], length[<min>, <max>], length[<min>, *]
charschars[<char 1>, <char 2>, ..., <char N>]Accept only specified characters.
emailemailValid email
urlurl, url[absolute]Valid URL. If absolute parameter is specified, the field value must have to be a full URL.
ipv4ipv4Valid IPv4
alpha_numericalpha_numericAccept only Alpha Numeric characters
alpha_dashalpha_dashAccept only Alpha characters and Dash ( - )
digitdigitChecks wheter a string consists of digits only (no dots or dashes).
decimaldecimal, decimal[<digits>,<decimals>]
regexpregexp[/^regular expression$/]PCRE Regular Expression
match_fieldmatch_field[otherfield]Check if the field has same value of otherfield.
rangerange[<min>, <max>]Check if the field value is in defined range.

Available Filters

FilterDescription
numericRemove all non numeric characters.
trimRemove all spaces before and after value.
uppercaseTransform all characters to upper case.
lowercaseTransform all characters to lower case.
strip_tagsStrips out ALL html tags.
html_entitiesDecodes all previously encoded entities, and then encodes all entities.

Usage

Example:

<?php//...$form['myfield'] = array(
'#type' => 'textfield',
'#title' => 'My Field',
'#required' => TRUE,
'#validators' => array(
'email', 'length[10, 50]', array('rule' => 'alpha_numeric', 'error' => 'Please, use only alpha numeric characters at %field.'),
array('rule' => 'match_field[otherfield]', 'error callback' => 'mymodule_validation_error_msg'),
),
'#filters' => array('trim', 'uppercase')
);
//...functionmymodule_validation_error_msg($rule, $params, $element, $form_state) {
returnt("My custom error message for %field", array("%field" => $element['#title']));
}

Custom Validator / Filter

The Validator or Filter was built using the Drupal Plugin API, so to create your own custom validator or filter you should use the FapiValidationValidator or FapiValidationFilter Annontation and it respective implementation class.

Validators

First you have to create a file at src/Plugin/FapiValidationValidator/MyCustomValidator.php path of your module with the class implementation.

<?phpnamespaceDrupal\my_module\Plugin\FapiValidationValidator;
useDrupal\Core\Form\FormStateInterface;
useDrupal\fapi_validation\FapiValidationValidatorsInterface;
useDrupal\fapi_validation\Validator;
/** * Provides a custom validation. * * Field must have JonhDoe as value. * * @FapiValidationValidator( * id = "custom_validator", * error_message = "Type the text 'custom value' at field %field." * ) */class MyCustomValidator implements FapiValidationValidatorsInterface {
/** * {@inheritdoc} */publicfunctionvalidate(Validator$validator, array$element, FormStateInterface$form_state) {
return$validator->getValue() == 'custom value';
}
}

Now you are able to use at your form definiton.

//...$form['custom_field'] = [
'#type' => 'textfield',
'#title' => $this->t('Custom Field'),
'#description' => $this->t('The Value should be "custom value".'),
'#validators' => [
'custom_validator',
],
'#required' => TRUE,
];
//...

Processed Error Messages

If you need create a error message programaticaly, change the error_message key at annotation to error_callback with the public static method name.

//.../** * @FapiValidationValidator( * id = "custom_validator", * error_callback = "processError" * ) */class MyCustomValidator implements FapiValidationValidatorsInterface {
//.../** * Process custom error. * * @param Drupal\fapi_validation\Validator $validator * Validator. * @param array $element * Form element. * * @return string * Error message. */publicstaticfunctionprocessError(Validator$validator, array$element) {
$params = [
'%value' => $validator->getValue(),
'%field' => $element['#title'],
];
return\t("You must enter 'custom value' as value and not '%value' at field %field", $params);
}
}

Filters

First you have to create a file at src/Plugin/FapiValidationFilter/MyCustomFilter.php path of your module with the class implementation.

<?phpnamespaceDrupal\my_module\Plugin\FapiValidationFilter;
useDrupal\fapi_validation\FapiValidationFiltersInterface;
/** * Fapi Validation Plugin for remove Numeric filter. * * @FapiValidationFilter( * id = "custom_filter" * ) */class MyCustomFilter implements FapiValidationFiltersInterface {
/** * {@inheritdoc} */publicfunctionfilter($value) {
returnpreg_replace('/[^0-5]+/', '', $value);
}
}

Now you are able to use at your form definiton.

//...$form['custom_field'] = [
'#type' => 'textfield',
'#title' => $this->t('Custom Field'),
'#filters' => [
'custom_filter',
],
'#required' => TRUE,
];
//...

About

New Drupal 8 FAPI Validation module

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages