This module drastically increase the validation power of Drupal Form API (FAPI).
You can use the existent filters and rules or create your own. Is up to you.
So FAPI Validation come to give you, developer, more facility on your development process.
If you have any consideration to do, fill free for it at #637598: Merge with validation API?
And now you can use on your Form API.
You can use the existent filters and rules or create your own. Is up to you.
Why not Validation API ?
The Validation API module is focused on Drupal end users and not module developers. There is no documentation so I guess that you can only set validations on admin interface.So FAPI Validation come to give you, developer, more facility on your development process.
If you have any consideration to do, fill free for it at #637598: Merge with validation API?
Modules using FAPI Validation
Available Rules
Rule | Usage | Description |
---|---|---|
numeric | numeric |
Must contains only numbers. |
length | length[<total>] length[<min>, <max>] |
|
chars | chars[<char 1>, <char 2>, ..., <char N>] |
Accept only specified characters. |
email |
Valid email | |
url | url url[absolute] |
Valid URL. If absolute parameter is specified, the field value must have to be a full URL. |
ipv4 | ipv4 |
Valid IPv4 |
alpha_numeric | alpha_numeric |
Accept only Alpha Numeric characters |
alpha_dash | alpha_dash |
Accept only Alpha characters and Dash ( - ) |
digit | Checks wheter a string consists of digits only (no dots or dashes). | |
decimal | decimal decimal[<digits>,<decimals>] |
|
regexp | regexp[/^regular expression$/] |
PCRE Regular Expression |
Available Filters
Filter | Description |
---|---|
numeric | Remove all non numeric characters. |
trim | Remove all spaces before and after value. |
uppercase | Transform all characters to upper case. |
lowercase | Transform all characters to lower case. |
Usage
Example:<?php//...
$form['myfield'] = array(
'#type' => 'textfield',
'#title' => 'My Field',
'#required' => TRUE,
'#rules' => array(
'email',
'length[10, 50]',
array('rule' => 'alpha_numeric', 'error' => 'Please, use only alpha numeric characters at %field.')
),
'#filters' => array('trim', 'uppercase')
);
//...?>
Developer
If any of this rules was what you need, you can use your own with hook_fapi_validation_rules and hook_fapi_validation_filters.<?php/**
* Implementation of hook_fapi_validation_rules
*/function mymodule_fapi_validation_rules() {
return array(
'rule_name' => array(
'callback' => 'mymodule_validation_rule_name',
'error_msg' => 'Invalid value for %field'
),
);
}
/**
* Implementation of hook_fapi_validation_filters
*/function mymodule_fapi_validation_filters() {
return array(
'filter_name' => array(
'callback' => 'mymodule_validation_filter_name',
),
);
}
function mymodule_validation_rule_name($value) {
if (preg_match('/^[a-z]+$/', $value)) {
return TRUE;
}
else {
return FALSE;
}
}
function mymodule_validation_filter_name($value) {
return preg_replace('/\d+/', '', $value);
}?>
<?php//...
$form['myfield'] = array(
'#type' => 'textfield',
'#title' => 'Email',
'#required' => TRUE,
'#rules' => array('rule_name'),
'#filters' => array('trim', 'filter_name')
);
//...?>
No comments:
Post a Comment