clientside_validation.module 21.5 KB
Newer Older
Peter Droogmans's avatar
Peter Droogmans committed
1
<?php
Jelle's avatar
Jelle committed
2
// $Id$
Peter Droogmans's avatar
Peter Droogmans committed
3
4
/**
 * @file
5
 * Add client side validation to forms.
Peter Droogmans's avatar
Peter Droogmans committed
6
 */
7
8
9
define('CLIENTSIDE_VALIDATION_VALIDATE_ALL', 1);
define('CLIENTSIDE_VALIDATION_VALIDATE_SPECIFIC', 0);
define('CLIENTSIDE_VALIDATION_VALIDATE_ALL_EXCEPT', 2);
10
11
12

define('CLIENTSIDE_VALIDATION_EXCLUDE_PATHS', 0); //Add validation on all paths except those specified
define('CLIENTSIDE_VALIDATION_INCLUDE_PATHS', 1); //Only add validation on specified paths
Peter Droogmans's avatar
Peter Droogmans committed
13
14
15
16
/**
 * Implements hook_menu().
 */
function clientside_validation_menu() {
17
18
19
20
21
22
23
24
  $items['admin/config/validation'] = array(
    'title' => 'Validation',
    'description' => 'Validation settings.',
    'position' => 'right',
    'weight' => -10,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('clientside_validation_admin_settings'),
    'access arguments' => array('access administration pages'),
25
    'file' => 'clientside_validation.admin.inc',
26
27
  );
  $items['admin/config/validation/clientside_validation'] = array(
Peter Droogmans's avatar
Peter Droogmans committed
28
29
30
31
32
    'title' => 'Clientside Validation',
    'description' => 'Administer clientside validation.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('clientside_validation_admin_settings'),
    'access arguments' => array('administer site configuration'),
33
    'file' => 'clientside_validation.admin.inc',
Jelle Sebreghts's avatar
Jelle Sebreghts committed
34
  );
35
  $items['clientside_validation/ajax'] = array(
Jelle Sebreghts's avatar
Jelle Sebreghts committed
36
37
38
39
    'title' => 'Clientside validation ajax callback',
    'page callback' => '_clientside_validation_ajax_call',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
Peter Droogmans's avatar
Peter Droogmans committed
40
41
42
43
  );
  return $items;
}

Jelle Sebreghts's avatar
Jelle Sebreghts committed
44
function _clientside_validation_ajax_call() {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  $param = $_POST['param'];
  $value = $_POST['value'];
  $result = array();
  foreach ($param['expressions'] as $key => $regex) {
    if (!(bool) preg_match($regex, (string) $value)) {
      $result =  array('result' => FALSE, 'message' => $param['messages'][$key]);
      break;
    }
  }
  if (empty($result)) {
    $result = array('result' => TRUE);
  }
  drupal_json_output($result);
}

Peter Droogmans's avatar
Peter Droogmans committed
60
/**
61
 * Implementation of hook_form_alter().
Peter Droogmans's avatar
Peter Droogmans committed
62
 */
63
function clientside_validation_form_alter(&$form, &$form_state, $form_id) {
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  $page_match = TRUE;
  $path_filter = variable_get('clientside_validation_path_list', '');
  $in_or_exclude = variable_get('clientside_validation_filter_paths', CLIENTSIDE_VALIDATION_EXCLUDE_PATHS);
  if (!empty($path_filter)) {
    $path = drupal_get_path_alias($_GET['q']);
    // Compare with the internal and path alias (if any).
    $page_match = drupal_match_path($path, $path_filter);
    if ($path != $_GET['q']) {
      $page_match = $page_match || drupal_match_path($_GET['q'], $path_filter);
    }
    // When $in_or_exclude has a value of 0 (CLIENTSIDE_VALIDATION_EXCLUDE_PATHS),
    // Clientside Validation is added on all pages except those listed in $path_filter.
    // When set to 1 (CLIENTSIDE_VALIDATION_INCLUDE_PATHS), it is displayed only on
    // those pages listed in $path_filter.
    $page_match = !((bool)$in_or_exclude xor $page_match);
  }
  if(!$page_match) {
    return;
  }
83
84
  $validate_specific_setting = variable_get('clientside_validation_validate_specific', '');
  $validate_specific = preg_split('/(\r\n?|\n)/', $validate_specific_setting, -1, PREG_SPLIT_NO_EMPTY);
85
86
87
88
89
90
  if (variable_get('clientside_validation_validate_all', CLIENTSIDE_VALIDATION_VALIDATE_ALL) == CLIENTSIDE_VALIDATION_VALIDATE_SPECIFIC
    && !in_array($form_id, $validate_specific)) {
    return;
  }
  elseif (variable_get('clientside_validation_validate_all', CLIENTSIDE_VALIDATION_VALIDATE_ALL) == CLIENTSIDE_VALIDATION_VALIDATE_ALL_EXCEPT
    && in_array($form_id, $validate_specific)) {
91
92
    return;
  }
93
94
  if (strpos($form_id, 'webform_client_form') !== FALSE) {
    if (isset($form['actions']['previous'])) {
95
      $form['actions']['previous']['#attributes']['class'][] = 'cancel';
96
    }
97
    $form['#after_build'][] = 'clientside_validation_webform_after_build';
98
  }
99
  else {
100
101
102
    if (isset($form['actions']['delete'])) {
      $form['actions']['delete']['#attributes']['class'][] = 'cancel';
    }
103
104
105
106
107
108
109
110
111
112
    switch ($form['#id']) {
      case 'node-form':
      case 'webform-component-edit-form':
        $form['#after_build'][] = 'clientside_validation_form_after_build';
        break;
      default:
        $form['#after_build'][] = 'clientside_validation_form_after_build';
        break;
    }
  }
113
114
115
116
117
118
119
120

  // Add a setting for this form if the hidden elements should still be validated
  $include_hidden_setting = variable_get('clientside_validation_include_hidden', '');
  $include_hidden = preg_split('/(\r\n?|\n)/', $include_hidden_setting, -1, PREG_SPLIT_NO_EMPTY);
  if (in_array($form_id, $include_hidden)) {
    $setting['clientsideValidation']['forms'][$form['#id']]['includeHidden'] = TRUE;
    drupal_add_js($setting, 'setting');
  }
121
122
123
124
125
126
127
128
}

/**
 * Webform.
 */
function clientside_validation_webform_after_build(&$form, &$form_state) {
  $js_rules = array();
  drupal_alter("clientside_validation_webform", $form, $form_state, $js_rules);
Peter Droogmans's avatar
Peter Droogmans committed
129
  if (!empty($js_rules)) {
Jelle's avatar
Jelle committed
130
    $settings['clientsideValidation']['general'] = array(
Peter Droogmans's avatar
Peter Droogmans committed
131
132
      "errorClass" => "error",
      "wrapper" => "li",
Jelle Sebreghts's avatar
Jelle Sebreghts committed
133
      "validateTabs" =>  variable_get('clientside_validation_validate_tabs', 1),
134
135
      "scrollTo" => variable_get('clientside_validation_scrollto_errormessage', 1),
      "scrollSpeed" => (int)variable_get('clientside_validation_scroll_speed', 1000),
136
      "disableHtml5Validation" => variable_get('clientside_validation_disable_html5', 1),
137
138
139
      "validateOnBlur" => variable_get('clientside_validation_validate_onblur', 1),
      "validateOnKeyUp" => variable_get('clientside_validation_validate_onkeyup', 1),
      "validateOnSubmit" => variable_get('clientside_validation_validate_onsubmit', 1),
Peter Droogmans's avatar
Peter Droogmans committed
140
141
142
143
144
    );
    $settings['clientsideValidation']['forms'][$form['#id']]['settings'] = array(
      "errorContainer" => "#formerrors-" . $form['#id'],
      "errorLabelContainer" => "#formerrors-" . $form['#id'] . " ul",
    );
Jelle's avatar
Jelle committed
145
    foreach ($js_rules as $key => $rule) {
Peter Droogmans's avatar
Peter Droogmans committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
      if (isset($rule['checkboxgroupminmax'])) {
        $settings['clientsideValidation']['forms'][$form['#id']]['checkboxrules'][$key] = array(
            'checkboxgroupminmax' => $rule['checkboxgroupminmax'],
            'messages' => array('checkboxgroupminmax' => $rule['messages']['checkboxgroupminmax'])
        );
        unset($rule['checkboxgroupminmax']);
        unset($rule['messages']['checkboxgroupminmax']);
        if (empty($rule['messages'])) {
          unset($rule['messages']);
        }
      }
      if (!empty($rule)) {
        $settings['clientsideValidation']['forms'][$form['#id']]['rules'][$key] = $rule;
      }
Peter Droogmans's avatar
Peter Droogmans committed
160
    }
Peter Droogmans's avatar
Peter Droogmans committed
161
162
163
164
165
166
167
    
    if (variable_get('clientside_validation_use_minified', 0) == 0) {
      drupal_add_js(drupal_get_path('module', 'clientside_validation') . '/jquery-validate/jquery.validate.js');
    }
    else {
      drupal_add_js(drupal_get_path('module', 'clientside_validation') . '/jquery-validate/jquery.validate.min.js');
    }
168
169
    //add our js file to the footer to support vertical_tabs
    drupal_add_js(drupal_get_path('module', 'clientside_validation') . '/clientside_validation.js', array('scope' => 'footer'));
Peter Droogmans's avatar
Peter Droogmans committed
170
171
172
173
174
175
    drupal_add_js($settings, 'setting');
  }
  return $form;
}

/**
176
 * Regular form.
Peter Droogmans's avatar
Peter Droogmans committed
177
178
179
 */
function clientside_validation_form_after_build(&$form, &$form_state) {
  static $js_rules = array();
180
181
182
183
  static $form_ids = array();
  if (isset($form_ids[$form['#id']])) {
    return $form;
  }
184
  
185
  drupal_alter("clientside_validation_form", $form, $form_state, $js_rules);
Peter Droogmans's avatar
Peter Droogmans committed
186
  if (!empty($js_rules)) {
Jelle's avatar
Jelle committed
187
    $settings['clientsideValidation']['general'] = array(
Peter Droogmans's avatar
Peter Droogmans committed
188
189
      "errorClass" => "error",
      "wrapper" => "li",
190
      "validateTabs" =>  variable_get('clientside_validation_validate_tabs', 1),
191
192
      "scrollTo" => variable_get('clientside_validation_scrollto_errormessage', 1),
      "scrollSpeed" => (int)variable_get('clientside_validation_scroll_speed', 1000),
193
      "disableHtml5Validation" => variable_get('clientside_validation_disable_html5', 1),
194
195
196
      "validateOnBlur" => variable_get('clientside_validation_validate_onblur', 1),
      "validateOnKeyUp" => variable_get('clientside_validation_validate_onkeyup', 1),
      "validateOnSubmit" => variable_get('clientside_validation_validate_onsubmit', 1),
Peter Droogmans's avatar
Peter Droogmans committed
197
198
199
200
201
    );
    $settings['clientsideValidation']['forms'][$form['#id']]['settings'] = array(
      "errorContainer" => "#formerrors-" . $form['#id'],
      "errorLabelContainer" => "#formerrors-" . $form['#id'] . " ul",
    );
Jelle's avatar
Jelle committed
202
    foreach ($js_rules as $key => $rule) {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
203
204
205
206
207
208
209
210
211
212
213
214
215
216
      if (isset($rule['checkboxgroupminmax'])) {
        $settings['clientsideValidation']['forms'][$form['#id']]['checkboxrules'][$key] = array(
            'checkboxgroupminmax' => $rule['checkboxgroupminmax'],
            'messages' => array('checkboxgroupminmax' => $rule['messages']['checkboxgroupminmax'])
        );
        unset($rule['checkboxgroupminmax']);
        unset($rule['messages']['checkboxgroupminmax']);
        if (empty($rule['messages'])) {
          unset($rule['messages']);
        }
      }
      if (!empty($rule)) {
        $settings['clientsideValidation']['forms'][$form['#id']]['rules'][$key] = $rule;
      }
Peter Droogmans's avatar
Peter Droogmans committed
217
    }
Peter Droogmans's avatar
Peter Droogmans committed
218
219
220
221
222
223
    if (variable_get('clientside_validation_use_minified', 0) == 0) {
      drupal_add_js(drupal_get_path('module', 'clientside_validation') . '/jquery-validate/jquery.validate.js');
    }
    else {
      drupal_add_js(drupal_get_path('module', 'clientside_validation') . '/jquery-validate/jquery.validate.min.js');
    }
224
225
    //add our js file to the footer to support vertical_tabs
    drupal_add_js(drupal_get_path('module', 'clientside_validation') . '/clientside_validation.js', array('scope' => 'footer'));
Peter Droogmans's avatar
Peter Droogmans committed
226
    drupal_add_js($settings, 'setting');
227
    $form_ids[$form['#id']] = TRUE;
Peter Droogmans's avatar
Peter Droogmans committed
228
229
  }

230
  return $form;
Peter Droogmans's avatar
Peter Droogmans committed
231
232
}

233
234
235
/**
 * Set validation rule for required fields.
 */
Jelle's avatar
Jelle committed
236
function _clientside_validation_set_required($name, $title, $required, &$js_rules, $message = '') {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
237
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
Peter Droogmans's avatar
Peter Droogmans committed
238
239
  if ($required) {
    $js_rules[$name]['required'] = TRUE;
240
    $js_rules[$name]['messages']['required'] = (empty($message)) ? t('!name field is required.', array('!name' => $title)) : $message;
Peter Droogmans's avatar
Peter Droogmans committed
241
242
243
  }
}

244
245
246
/**
 * Set validation rule for number fields.
 */
247
function _clientside_validation_set_number($name, $title, &$js_rules) {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
248
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
Peter Droogmans's avatar
Peter Droogmans committed
249
250
251
252
  $js_rules[$name]['digits_negative'] = TRUE;
  $js_rules[$name]['messages']['digits_negative'] = t('!name field accepts only numbers.', array('!name' => $title));
}

253
254
255
/**
 * Set validation rule for decimal fields.
 */
256
function _clientside_validation_set_number_decimal($name, $title, $decimalpoint, &$js_rules) {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
257
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
Peter Droogmans's avatar
Peter Droogmans committed
258
259
260
261
262
263
264
265
266
267
  if ($decimalpoint == '.') {
    $js_rules[$name]['number'] = TRUE;
    $js_rules[$name]['messages']['number'] = t('!name field accepts only numbers (use a \'.\' as decimal point).', array('!name' => $title));
  }
  else {
    $js_rules[$name]['numberDE'] = TRUE;
    $js_rules[$name]['messages']['numberDE'] = t('!name field accepts only numbers (use a \',\' as decimal point).', array('!name' => $title));
  }
}

268
269
270
/**
 * Set validation rule for fields with a minimum and/or a maximum value.
 */
Jelle's avatar
Jelle committed
271
function _clientside_validation_set_minmax($name, $title, $min, $max, &$js_rules, $message = '') {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
272
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
273
  if (isset($min) && $min !== '' && isset($max) && $max !== '') {
Peter Droogmans's avatar
Peter Droogmans committed
274
    $js_rules[$name]['range'] = array($min, $max);
275
    $js_rules[$name]['messages']['range'] = (empty($message)) ? t('!name field has to be between !min and !max.', array('!name' => $title, '!min' => $min, '!max' => $max)) : $message;
Peter Droogmans's avatar
Peter Droogmans committed
276
  }
277
  elseif (isset($min) && $min !== '') {
Peter Droogmans's avatar
Peter Droogmans committed
278
    $js_rules[$name]['min'] = $min;
279
    $js_rules[$name]['messages']['min'] = (empty($message)) ? t('!name field has to be greater than !min.', array('!name' => $title, '!min' => $min)) : $message;
Peter Droogmans's avatar
Peter Droogmans committed
280
  }
281
  elseif (isset($max) && $max !== '') {
Peter Droogmans's avatar
Peter Droogmans committed
282
    $js_rules[$name]['max'] = $max;
283
    $js_rules[$name]['messages']['max'] = (empty($message)) ? t('!name field has to be smaller than !max.', array('!name' => $title, '!max' => $max)) : $message;
Peter Droogmans's avatar
Peter Droogmans committed
284
285
286
  }
}

287
288
289
/**
 * Set validation rule for fields with a minimum and/or maximum length.
 */
Jelle's avatar
Jelle committed
290
function _clientside_validation_set_minmaxlength($name, $title, $min, $max, &$js_rules, $message = '') {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
291
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
Jelle Sebreghts's avatar
Jelle Sebreghts committed
292
  if ($max == 1) {
293
294
    $max = '';
  }
295
  if (isset($min) && $min !== '' && isset($max) && $max !== '') {
Peter Droogmans's avatar
Peter Droogmans committed
296
    $js_rules[$name]['rangelength'] = array($min, $max);
297
    $js_rules[$name]['messages']['rangelength'] = (empty($message)) ? t('!name field has to have between !min and !max values.', array('!name' => $title, '!min' => $min, '!max' => $max)) : $message;
Peter Droogmans's avatar
Peter Droogmans committed
298
  }
299
  elseif (isset($min) && $min !== '') {
Peter Droogmans's avatar
Peter Droogmans committed
300
    $js_rules[$name]['minlength'] = $min;
301
    $js_rules[$name]['messages']['minlength'] = (empty($message)) ? t('!name field has to have minimal !min values.', array('!name' => $title, '!min' => $min)) : $message;
Peter Droogmans's avatar
Peter Droogmans committed
302
  }
303
  elseif (isset($max) && $max !== '') {
Peter Droogmans's avatar
Peter Droogmans committed
304
    $js_rules[$name]['maxlength'] = $max;
305
    $js_rules[$name]['messages']['maxlength'] = (empty($message)) ? t('!name field has to have maximum !max values.', array('!name' => $title, '!max' => $max)) : $message;
Peter Droogmans's avatar
Peter Droogmans committed
306
307
308
  }
}

309
310
311
/**
 * Set validation rule for required fields that must equal a value from an other field.
 */
Jelle's avatar
Jelle committed
312
function _clientside_validation_set_equal($name, $title, $value, &$js_rules) {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
313
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
314
315
316
317
318
319
320
  $js_rules[$name]['equalTo'] = ':input[name=\'submitted[' . $value['form_key'] . ']\']';
  $js_rules[$name]['messages']['equalTo'] = t('!name field has to be equal to !firstone.', array('!name' => $title, '!firstone' => $value['name']));
}

/**
 * Set validation rule for fields that can not be equal to a value from an other field.
 */
Jelle's avatar
Jelle committed
321
function _clientside_validation_set_not_equal($name, $title, $value, &$js_rules, $message = '') {
322
  $js_rules[$name]['notEqualTo'] = ':input[name=\'submitted[' . $value['form_key'] . ']\']';
Jelle Sebreghts's avatar
Jelle Sebreghts committed
323
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
324
325
326
327
328
329
330
  $message = empty($message) ? t('!name field has to different from !firstone.', array('!name' => $title, '!firstone' => $value['name'])) : $message;
  $js_rules[$name]['messages']['notEqualTo'] = $message;
}

/**
 * Set validation rule for fields that must be equal to a specific value.
 */
Jelle's avatar
Jelle committed
331
function _clientside_validation_set_specific_value($name, $title, $value, &$js_rules, $message = '') {
332
  $js_rules[$name]['oneOf'] = $value;
Jelle Sebreghts's avatar
Jelle Sebreghts committed
333
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
334
335
336
337
  $message = empty($message) ? t('!name field has to different from !firstone.', array('!name' => $title, '!firstone' => $value['name'])) : $message;
  $js_rules[$name]['messages']['oneOf'] = $message;
}

338
339
340
341
342
343
344
345
346
347
/**
 * Set validation rule for fields that consist of one or more of specific values (e.g. string with only a, b and c in it).
 */
function _clientside_validation_set_specific_values($name, $title, $values, &$js_rules, $message = '') {
  $js_rules[$name]['specificVals'] = $values;
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
  $message = empty($message) ? t('!name field must consist of following elements only: !elements.', array('!name' => $title, '!elements' => implode(', ', $values))) : $message;
  $js_rules[$name]['messages']['specificVals'] = $message;
}

348
349
350
/**
 * Set validation rule for ean number fields.
 */
Jelle's avatar
Jelle committed
351
function _clientside_validation_set_ean($name, $title, &$js_rules, $message = '') {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
352
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
353
354
  $message = empty($message) ? t('!name field is not a valid EAN number.', array('!name' => $title)) : $message;
  $js_rules[$name]['validEAN'] = TRUE;
Peter Droogmans's avatar
Peter Droogmans committed
355
  $js_rules[$name]['messages']['validEAN'] = $message;
Peter Droogmans's avatar
Peter Droogmans committed
356
357
}

358
359
360
/**
 * Set validation rule for fields with regex validation from webform_validation
 */
361
function _clientside_validation_set_regex($name, $title, &$js_rules, $expression, $message = '', $modifiers = "") {
362
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
363
  $message = empty($message) ? t('!name field is not match the given format.', array('!name' => $title)) : $message;
364
  $js_rules[$name]['regexMatch'] = array($expression);
Jelle Sebreghts's avatar
Jelle Sebreghts committed
365
  if (!empty($modifiers)) {
366
367
    $js_rules[$name]['regexMatch'][] = $modifiers;
  }
368
369
370
  $js_rules[$name]['messages']['regexMatch'] = $message;
}

Jelle Sebreghts's avatar
Jelle Sebreghts committed
371
372
373
374
375
376
377
378
379
380
/**
 * Set validation rule for fields with regex validation from webform_validation
 */
function _clientside_validation_set_regex_pcre($name, $title, &$js_rules, $expression, $message = '') {
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
  $message = empty($message) ? t('!name field is not a valid EAN number.', array('!name' => $title)) : $message;
  $js_rules[$name]['regexMatchPCRE']['expressions'][] = $expression;
    $js_rules[$name]['regexMatchPCRE']['messages'][] = $message;
}

381
382
383
/**
 * Set validation rule for file fields that must have a certain extension.
 */
Jelle's avatar
Jelle committed
384
function _clientside_validation_set_extensions($name, $extensions, &$js_rules) {
385
386
387
388
389
390
391
392
  $extension_list = preg_replace('#,(?![^,]+,)#', ' or', implode(', ', $extensions));
  $js_rules[$name]['accept'] = implode('|', $extensions);
  $js_rules[$name]['messages']['accept'] = t("Only files with a %exts extension are allowed.", array('%exts' => $extension_list));
}

/**
 * Set validation rule for checkboxes.
 */
393
function _clientside_validation_set_checkboxgroup_minmax($name, $title, $id, &$js_rules, $message = '', $min = 1, $max = 99) {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
394
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
395
  $message = empty($message) ? t('!name field is required.', array('!name' => $title)) : $message;
396
  $js_rules[$name]['checkboxgroupminmax'] = array($min, $max, $id);
397
  $js_rules[$name]['messages']['checkboxgroupminmax'] = $message;
398
399
400
401
402
}

/**
 * Set validation rule for email fields.
 */
Jelle's avatar
Jelle committed
403
function _clientside_validation_set_email($name, $title, &$js_rules) {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
404
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
405
406
407
408
  $js_rules[$name]['email'] = TRUE;
  $js_rules[$name]['messages']['email'] = t('The value in !name is not a valid email address.', array('!name' => $title));
}

409
410
411
/**
 * Set validation rule for url fields.
 */
Jelle Sebreghts's avatar
Jelle Sebreghts committed
412
function _clientside_validation_set_url($name, $title, &$js_rules) {
413
414
415
416
417
418
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
  $js_rules[$name]['url'] = TRUE;
  $js_rules[$name]['messages']['url'] = t('The value in !name is not a valid url.', array('!name' => $title));
}

/**
Jelle Sebreghts's avatar
Jelle Sebreghts committed
419
 * Set fapi validation.
420
 */
Jelle Sebreghts's avatar
Jelle Sebreghts committed
421
function _clientside_validation_set_fapi_validation($name, $title, $callback, $params, &$js_rules, $message = '') {
422
423
424
425
426
427
  if (strpos($message, '%field') !== FALSE) {
    $message = t($message, array('%field' => $title));
  }
  else {
    $message = t($message);
  }
428
  $expressions = array(
Jelle Sebreghts's avatar
Jelle Sebreghts committed
429
430
431
432
433
434
    'fapi_validation_rule_alpha' => '/^[\pL]++$/uD',
    'fapi_validation_rule_alpha_numeric' => '/^[\pL\pN]++$/uD',
    'fapi_validation_rule_ipv4' => '/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
      . '(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/',
    'fapi_validation_rule_alpha_dash' => '/^[-\pL\pN_]+$/uD',
    'fapi_validation_rule_digit' => '/^\d+$/',
435
436
437
438
439
440
441
442
  );
  switch ($callback) {
    case 'fapi_validation_rule_alpha':
    case 'fapi_validation_rule_alpha_numeric':
    case 'fapi_validation_rule_ipv4':
    case 'fapi_validation_rule_alpha_dash':
    case 'fapi_validation_rule_digit':
    case 'fapi_validation_rule_regexp':
Jelle Sebreghts's avatar
Jelle Sebreghts committed
443
      if ($callback == 'fapi_validation_rule_regexp') {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
444
        $expressions['fapi_validation_rule_regexp'] = $params[1][0];
445
      }
Jelle Sebreghts's avatar
Jelle Sebreghts committed
446
      _clientside_validation_set_regex_pcre($name, $title, $js_rules, $expressions[$callback], $message);
447
448
449

      break;
    case 'fapi_validation_rule_numeric':
450
      _clientside_validation_set_number($name, $title, $js_rules);
451
452
453
454
455
      break;
    case 'fapi_validation_rule_email':
      _clientside_validation_set_email($name, $title, $js_rules);
      break;
    case 'fapi_validation_rule_length':
Jelle Sebreghts's avatar
Jelle Sebreghts committed
456
      if (count($params[1]) == 1) {
457
458
        _clientside_validation_set_minmaxlength($name, $title, '', $params[1][0], $js_rules);
      }
Jelle Sebreghts's avatar
Jelle Sebreghts committed
459
      elseif (count($params[1]) == 2) {
460
461
462
463
464
465
466
467
468
469
        _clientside_validation_set_minmaxlength($name, $title, $params[1][0], $params[1][1], $js_rules);
      }
      break;
    case 'fapi_validation_rule_url':
      _clientside_validation_set_url($name, $title, $js_rules);
      break;
    case 'fapi_validation_rule_chars':
      _clientside_validation_set_specific_values($name, $title, $params[1], $js_rules);
      break;
    case 'fapi_validation_rule_decimal':
Jelle Sebreghts's avatar
Jelle Sebreghts committed
470
471
      if (count($params[1]) == 2) {
        $expression = '/^[0-9]{' . $params[1][0] . '}\.[0-9]{' . $params[1][1] . '}$/';
Jelle Sebreghts's avatar
Jelle Sebreghts committed
472
473
474
475
476
      }
      else {
        $expression = '/\d+\.\d+/';
      }
      _clientside_validation_set_regex_pcre($name, $title, $js_rules, $expression, $message);
477
478
479
480
481
482
      break;
    default:
      break;
  }
}

483
484
485
function clientside_validation_drupal_json_encode($var) {
  return str_replace(array('<', '>', '&'), array('\u003c', '\u003e', '\u0026'), json_encode($var));
}