-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpvalidator.module
52 lines (42 loc) · 1.6 KB
/
cpvalidator.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
/**
* Implementation of hook_webform_validation_validators().
*/
function cpvalidator_webform_validation_validators() {
return
array(
'validate_spanish_cp' => array(
'name' => 'Validates a spanish postal code',
'description' => t( 'Verifies the existence of this spanish postal code.' ),
'component_types' => array( 'textfield' ) )
);
}
/**
* Implementation of hook_webform_validation_validate().
*/
function cpvalidator_webform_validation_validate($validator_name, $items, $components, $rule ) {
// List of CP
require_once("postalcodes.php");
// Errors
$error = 'El código postal tiene algún error, por favor revísalo';
$numeric_error = 'El código postal debe ser un valor numérico';
$errors = array();
if ( $items ) {
switch ( $validator_name ) {
case 'validate_spanish_cp':
foreach ($items as $key => $value) {
if(!preg_match('/[0-9]+/', $value)){
$errors[$key] = t( $numeric_error,
array( '%item' => $components[ $key ][ "name" ] ) );
}
else if( !in_array($value, $postal_codes) ) {
$errors[$key] = t( $error,
array( '%item' => $components[ $key ][ "name" ] ) );
}
return $errors;
}
break;
}
}
}
?>