Issue #1419078 by lucashedding: Fixed Call to undefined function clientside_validatio...
[project/clientside_validation.git] / clientside_validation_fapi / clientside_validation_fapi.module
1 <?php
2 // $Id$
3 /**
4 * @file
5 * Add client side validation support for fapi validation
6 */
7
8 function clientside_validation_fapi_clientside_validation_form_alter(&$form, &$form_state, &$js_rules) {
9 clientside_validation_fapi_after_build_recurse($form['#id'], $form, $form_state, $js_rules);
10 }
11
12 function clientside_validation_fapi_after_build_recurse($form_id, &$form, &$form_state, &$js_rules) {
13 if ($children = array_values(element_children($form))) {
14 foreach ($children as $index => $item) {
15 $element = &$form[$item];
16 $types = array(
17 'textfield', 'textarea', 'select', 'radio', 'checkbox', 'password', 'file', 'radios', 'checkboxes',
18 );
19 if (isset($element['#type']) && in_array($element['#type'], $types)) {
20 clientside_validation_fapi_regular($form_id, $element, $js_rules);
21 }
22 clientside_validation_fapi_after_build_recurse($form_id, $element, $form_state, $js_rules);
23 }
24 }
25 }
26
27 function clientside_validation_fapi_regular($form_id, $element, &$js_rules) {
28 if (isset($element['#name']) && isset($element['#rules'])) {
29 $el_name = $element['#name'];
30 $el_title = $el_name;
31 if (isset($element['#title'])) {
32 $el_title = $element['#title'];
33 }
34 $data = _fapi_validation_data('rules');
35 foreach ($element['#rules'] as $rule) {
36 $params = array($element['#value']);
37 $error_message = NULL;
38
39 // If $rule is an array, use error message if is set.
40 if (is_array($rule)) {
41 if (isset($rule['error'])) {
42 $error_message = $rule['error'];
43 }
44
45 if (!isset($rule['rule'])) {
46 drupal_set_message(t('Rule array with wrong structure on %field.', array('%field' => $element['#name'])), 'error');
47 continue;
48 }
49 $rule = $rule['rule'];
50 }
51
52 preg_match('/^(.*?)(\[(.*)\])?$/', $rule, $rs);
53
54 $rule = $rs[1];
55
56 if (!isset($data[$rule])) {
57 $error_message = t('Rule %rule not found!', array('%rule' => $rule));
58 continue;
59 }
60
61 // Parsing parameters.
62 if (isset($rs[3])) {
63 $params[] = preg_split('/ *, */', $rs[3]);
64 }
65 if (count($params) > 1) {
66 $params = $params[1];
67 }
68 $error = is_null($error_message) ? $data[$rule]['error_msg'] : $error_message;
69 _clientside_validation_set_fapi_validation($element, $data[$rule], $params, $js_rules, $error);
70 }
71 }
72 }
73
74 /**
75 * Set fapi validation.
76 */
77 function _clientside_validation_set_fapi_validation($element, $rule, $params, &$js_rules, $message = '') {
78 $callback = $rule['callback'];
79 $name = $element['#name'];
80 $title = $name;
81 if (isset($element['#title'])) {
82 $title = $element['#title'];
83 }
84 $expressions = array(
85 'fapi_validation_rule_alpha' => '/^[\pL]++$/uD',
86 'fapi_validation_rule_alpha_numeric' => '/^[\pL\pN]++$/uD',
87 'fapi_validation_rule_ipv4' => '/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
88 . '(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/',
89 'fapi_validation_rule_alpha_dash' => '/^[-\pL\pN_]+$/uD',
90 'fapi_validation_rule_digit' => '/^\d+$/',
91 );
92 switch ($callback) {
93 case 'fapi_validation_rule_alpha':
94 $type = 'alpha';
95 case 'fapi_validation_rule_alpha_numeric':
96 $type = 'alpha numeric';
97 case 'fapi_validation_rule_ipv4':
98 $type = 'ipv4';
99 case 'fapi_validation_rule_alpha_dash':
100 $type = 'alpha dash';
101 case 'fapi_validation_rule_digit':
102 $type = 'digit';
103 case 'fapi_validation_rule_regexp':
104 if ($callback == 'fapi_validation_rule_regexp') {
105 $expressions['fapi_validation_rule_regexp'] = $params[1][0];
106 $type = 'regexp';
107 }
108 if (strpos($message, '%field') !== FALSE) {
109 $message = t($message, array('%field' => variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '')));
110 }
111 _clientside_validation_set_regex_pcre($name, $title, $js_rules, $expressions[$callback], $message, $type);
112
113 break;
114 case 'fapi_validation_rule_numeric':
115 _clientside_validation_set_number_decimal($name, $title, '.', $js_rules);
116 break;
117 case 'fapi_validation_rule_email':
118 _clientside_validation_set_email($name, $title, $js_rules);
119 break;
120 case 'fapi_validation_rule_length':
121 if (count($params) == 1) {
122 _clientside_validation_set_minmaxlength($name, $title, '', $params[0], $js_rules);
123 }
124 elseif (count($params) == 2) {
125 _clientside_validation_set_minmaxlength($name, $title, $params[0], $params[1], $js_rules);
126 }
127 break;
128 case 'fapi_validation_rule_url':
129 _clientside_validation_set_url($name, $title, $js_rules);
130 break;
131 case 'fapi_validation_rule_chars':
132 _clientside_validation_set_specific_values($name, $title, $params, $js_rules);
133 break;
134 case 'fapi_validation_rule_decimal':
135 if (count($params) == 2) {
136 $expression = '/^[0-9]{' . $params[0] . '}\.[0-9]{' . $params[1] . '}$/';
137 }
138 else {
139 $expression = '/\d+\.\d+/';
140 }
141 if (strpos($message, '%field') !== FALSE) {
142 $message = t($message, array('%field' => variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '')));
143 }
144 _clientside_validation_set_regex_pcre($name, $title, $js_rules, $expression, $message, 'decimal');
145 break;
146 default:
147 $context = array('type' => 'fapi', 'rule' => $rule, 'message' => $message, 'params' => $params);
148 drupal_alter('clientside_validation_rule', $js_rules, $element, $context);
149 break;
150 }
151 }