How to write a validator for field validation 7.x-2.x
In field validation 7.x-2.x, validator is a plugin of Ctools, it is very easy to write your own validator, here is an example :
<?php
/**
* @file
* Field validation email validator.
*
*/
$plugin = array(
'label' => t('Email'),
'description' => t("Verifies that user-entered data is a valid email address."),
'handler' => array(
'class' => 'field_validation_email_validator',
),
);
class field_validation_email_validator extends field_validation_validator {
/**
* Validate field.
*/
public function validate() {
if ($this->value != '' && (!valid_email_address($this->value))) {
$this->set_error();
}
}
}
It is very simple, this is the email validator shipped with field-validation 7.x-2.0-beta2.
You could copy a validator from field validation, and edit it to meet your requirement.
If you want to set some argument for your validator , here is another example:
$plugin = array(
'label' => t('Numeric values'),
'description' => t('Verifies that user-entered values are numeric, with the option to specify min and / or max values.'),
'handler' => array(
'class' => 'field_validation_numeric2_validator',
),
);
class field_validation_numeric2_validator extends field_validation_validator {
/**
* Validate field.
*/
public function validate() {
$settings = $this->rule->settings;
if ($this->value != '') {
$flag = TRUE;
if (!is_numeric($this->value)) {
$flag = FALSE;
}
else{
if (isset($settings['min']) && $settings['min'] != '' && $this->value < $settings['min']) {
$flag = FALSE;
}
if (isset($settings['max']) && $settings['max'] != '' && $this->value > $settings['max']) {
$flag = FALSE;
}
}
if (!$flag) {
$token = array(
'[min]' => isset($settings['min']) ? $settings['min'] : '',
'[max]' => isset($settings['max']) ? $settings['max'] : '',
);
$this->set_error($token);
}
}
}
/**
* Provide settings option
*/
function settings_form(&$form, &$form_state) {
$default_settings = $this->get_default_settings($form, $form_state);
//print debug($default_settings);
$form['settings']['min'] = array(
'#title' => t('Minimum value'),
'#description' => t("Optionally specify the minimum value to validate the user-entered numeric value against."),
'#type' => 'textfield',
'#default_value' => isset($default_settings['min']) ? $default_settings['min'] : '',
);
$form['settings']['max'] = array(
'#title' => t('Maximum value'),
'#description' => t("Optionally specify the maximum value to validate the user-entered numeric value against."),
'#type' => 'textfield',
'#default_value' => isset($default_settings['max']) ? $default_settings['max'] : '',
);
parent::settings_form($form, $form_state);
}
/**
* Provide token help info for error message.
*/
public function token_help() {
$token_help = parent::token_help();
$token_help += array(
'[min]' => t('Minimum value'),
'[max]' => t('Maximum value'),
);
return $token_help;
}
}
This is the numeric validator shipped with field validation.
For a custom validator, create a custom module and add these lines to the .module file:
<?php
function mymodule_ctools_plugin_directory($module, $plugin) {
if ($module == 'field_validation' && $plugin == 'validator') {
return 'plugins/' . $plugin;
}
}
Then add the following folder inside the module:
plugins
and
plugins/validator
Place the validator file insite the validator folder and clear your cache.
Now you should be able to use it via the UI / validators.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion