forked from amnesty/drupal-nif-nie-cif-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nif_nie_cif_validator.module
149 lines (134 loc) · 6.84 KB
/
nif_nie_cif_validator.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
require_once( 'includes/nif-nie-cif.php' );
/**
* Implementation of hook_webform_validation_validators().
*/
function nif_nie_cif_validator_webform_validation_validators() {
return
array(
'validate_spanish_ids' => array(
'name' => 'Validate NIF/NIE/CIF',
'description' => t( 'Verifies that a user-entered value matches a NIF/NIE/CIF and verifies it\'s check digit.' ),
'component_types' => array( 'textfield' ) ),
'validate_nif' => array(
'name' => 'Validate NIF',
'description' => t( 'Verifies that a user-entered value matches a NIF and verifies it\'s check digit.' ),
'component_types' => array( 'textfield' ) ),
'validate_nie' => array(
'name' => 'Validate NIE',
'description' => t( 'Verifies that a user-entered value matches a NIE and verifies it\'s check digit.' ),
'component_types' => array( 'textfield' ) ),
'validate_cif' => array(
'name' => 'Validate CIF',
'description' => t( 'Verifies that a user-entered value matches a CIF and verifies it\'s check digit.' ),
'component_types' => array( 'textfield' ) ),
'validate_ids_separately' => array(
'name' => 'Validate NIF/NIE/CIF separately',
'description' => t( 'Verifies that a user-entered value matches a NIF/NIE/CIF, depending on the option selected, and then verifies it\'s check digit.' ),
'component_types' => array( 'select','textfield' ) ),
);
}
/**
* Implementation of hook_webform_validation_validate().
*/
function nif_nie_cif_validator_webform_validation_validate($validator_name, $items, $components, $rule) {
$errors = array();
if ( $items ) {
switch ( $validator_name ) {
case 'validate_spanish_ids':
foreach ( $items as $key => $value ) {
if ( $value && ( !isValidIdNumber( $value ) ) ) {
$errors[$key] = t( '%item is not a valid Spanish Id', array( '%item' => $components[$key]['name'] ) );
}
}
return $errors;
break;
case 'validate_nif':
foreach ( $items as $key => $value ) {
if ( $value && ( !isValidNIF( $value ) ) ) {
$errors[$key] = t( '%item is not a valid NIF', array( '%item' => $components[$key]['name'] ) );
}
}
return $errors;
break;
case 'validate_nie':
foreach ( $items as $key => $value ) {
if ( $value && ( !isValidNIE( $value ) ) ) {
$errors[$key] = t( '%item is not a valid NIE', array( '%item' => $components[$key]['name'] ) );
}
}
return $errors;
break;
case 'validate_cif':
foreach ( $items as $key => $value ) {
if ( $value && ( !isValidCIF( $value ) ) ) {
$errors[$key] = t( '%item is not a valid CIF', array( '%item' => $components[$key]['name'] ) );
}
}
return $errors;
break;
case 'validate_ids_separately':
$tipo = "";
$numero = "";
foreach ( $items as $key => $value ) {
// Get the component class to take its value
$component = $components[$key];
$class_exp = explode(" ", $component["extra"]["css_classes"]);
$class = $class_exp[0];
switch( $class ){
case "tipo_documento":
$tipo = $value;
break;
case "numero_documento":
$numero = $value;
break;
}
}
// depending on which type of ID, we do a validation or other
switch ($tipo) {
case 'nif':
if ( $numero && ( !isValidNIF( $numero ) ) ) {
$i=0;
// Mark the second box as an error, that is the other amount one
foreach( $items as $key => $value){
if($i==1){
$errors[$key] = t( '¿Puedes repasar tu número de documento de identidad? Hemos detectado algún error.',
array( '%item' => $components[ $key ][ "name" ] ) );
}
$i++;
}
}
break;
case 'nie':
if ( $numero && ( !isValidNIE( $numero ) ) ) {
$i=0;
// Mark the second box as an error, that is the other amount one
foreach( $items as $key => $value){
if($i==1){
$errors[$key] = t( '¿Puedes repasar tu NIE? Hemos detectado algún error.',
array( '%item' => $components[ $key ][ "name" ] ) );
}
$i++;
}
}
break;
case 'cif':
if ( $numero && ( !isValidCIF( $numero ) ) ) {
$i=0;
// Mark the second box as an error, that is the other amount one
foreach( $items as $key => $value){
if($i==1){
$errors[$key] = t( '¿Puedes repasar tu CIF? Hemos detectado algún error.',
array( '%item' => $components[ $key ][ "name" ] ) );
}
$i++;
}
}
break;
}
return $errors;
break;
}
}
}
?>