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

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
12
13
14
15
16
17
18
19

define('CLIENTSIDE_VALIDATION_JQUERY_SELECTOR', 0);
define('CLIENTSIDE_VALIDATION_TOP_OF_FORM', 1);
define('CLIENTSIDE_VALIDATION_BEFORE_LABEL', 2);
define('CLIENTSIDE_VALIDATION_AFTER_LABEL', 3);
define('CLIENTSIDE_VALIDATION_BEFORE_INPUT', 4);
define('CLIENTSIDE_VALIDATION_AFTER_INPUT', 5);
define('CLIENTSIDE_VALIDATION_TOP_OF_FIRST_FORM', 6);
20
define('CLIENTSIDE_VALIDATION_CUSTOM_ERROR_FUNCTION', 7);
21

Peter Droogmans's avatar
Peter Droogmans committed
22
23
24
25
/**
 * Implements hook_menu().
 */
function clientside_validation_menu() {
26
27
28
29
30
31
  $items['admin/config/validation'] = array(
    'title' => 'Validation',
    'description' => 'Validation settings.',
    'position' => 'right',
    'weight' => -10,
    'page callback' => 'drupal_get_form',
32
    'page arguments' => array('clientside_validation_general_settings_form'),
33
    'access arguments' => array('access administration pages'),
34
    'file' => 'clientside_validation.admin.inc',
35
36
  );
  $items['admin/config/validation/clientside_validation'] = array(
Peter Droogmans's avatar
Peter Droogmans committed
37
38
39
    'title' => 'Clientside Validation',
    'description' => 'Administer clientside validation.',
    'page callback' => 'drupal_get_form',
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    'page arguments' => array('clientside_validation_general_settings_form'),
    'access arguments' => array('administer site configuration'),
    'file' => 'clientside_validation.admin.inc',
  );
  $items['admin/config/validation/clientside_validation/general'] = array(
    'title' => 'General',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -1,
  );
  $items['admin/config/validation/clientside_validation/default'] = array(
    'title' => 'Default',
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('clientside_validation_settings_form'),
Peter Droogmans's avatar
Peter Droogmans committed
54
    'access arguments' => array('administer site configuration'),
55
    'file' => 'clientside_validation.admin.inc',
Jelle Sebreghts's avatar
Jelle Sebreghts committed
56
  );
57
  $items['clientside_validation/ajax'] = array(
Jelle Sebreghts's avatar
Jelle Sebreghts committed
58
59
60
61
    '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
62
  );
63
64
65
66
67
68
  $items['clientside_validation/phone'] = array(
    'title' => 'Clientside validation ajax callback to validate phone numbers',
    'page callback' => '_clientside_validation_ajax_phone',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
Peter Droogmans's avatar
Peter Droogmans committed
69
70
71
  return $items;
}

Jelle Sebreghts's avatar
Jelle Sebreghts committed
72
function _clientside_validation_ajax_call() {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  $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);
}

88
89
90
91
92
93
94
function _clientside_validation_ajax_phone() {
  $value = $_POST['value'];
  $country_code = $_POST['country_code'];
  $result['result'] = valid_phone_number($country_code, $value);
  drupal_json_output($result);
}

Peter Droogmans's avatar
Peter Droogmans committed
95
/**
Jelle Sebreghts's avatar
Jelle Sebreghts committed
96
 * Implements hook_form_alter().
Peter Droogmans's avatar
Peter Droogmans committed
97
 */
98
function clientside_validation_form_alter(&$form, &$form_state, $form_id) {
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  $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);
  }
Jelle Sebreghts's avatar
Jelle Sebreghts committed
115
  if (!$page_match) {
116
117
    return;
  }
118
119
  $validate_specific_setting = variable_get('clientside_validation_validate_specific', '');
  $validate_specific = preg_split('/(\r\n?|\n)/', $validate_specific_setting, -1, PREG_SPLIT_NO_EMPTY);
120
121
122
123
124
125
  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)) {
126
127
    return;
  }
128
129
  if (strpos($form_id, 'webform_client_form') !== FALSE) {
    if (isset($form['actions']['previous'])) {
130
      $form['actions']['previous']['#attributes']['class'][] = 'cancel';
131
    }
132
    $form['#after_build'][] = 'clientside_validation_webform_after_build';
133
  }
134
  else {
135
136
137
    if (isset($form['actions']['delete'])) {
      $form['actions']['delete']['#attributes']['class'][] = 'cancel';
    }
138
    $form['#after_build'][] = 'clientside_validation_form_after_build';
139
140
141
142
143
144
145
146
147
  }
}

/**
 * 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
148
  if (!empty($js_rules)) {
149
    $settings = array();
150
    _clientside_validation_add_general_settings($settings, $form['#id']);
151
    _clientside_validation_add_special_rules($js_rules, $settings, $form);
Jelle Sebreghts's avatar
Jelle Sebreghts committed
152
153


Peter Droogmans's avatar
Peter Droogmans committed
154
155
156
157
158
159
    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');
    }
160
    //add our js file to the footer to support vertical_tabs
161
    drupal_add_js(drupal_get_path('module', 'clientside_validation') . '/clientside_validation.js', array('scope' => 'footer', 'weight' => 20));
Peter Droogmans's avatar
Peter Droogmans committed
162
163
164
165
166
    drupal_add_js($settings, 'setting');
  }
  return $form;
}

167
function _clientside_validation_add_special_rules(&$js_rules, &$settings, $form) {
168
  if (isset($js_rules['_groups'])) {
169
    $settings['clientsideValidation']['groups'][$form['#id']] = (object)$js_rules['_groups'];
170
171
172
    unset($js_rules['_groups']);
  }
  else {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
173
    $settings['clientsideValidation']['groups'][$form['#id']] = new stdClass();
174
  }
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
  foreach ($js_rules as $key => $rule) {
    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 (isset($rule['daterange'])) {
      $settings['clientsideValidation']['forms'][$form['#id']]['daterangerules'][$key] = array(
          'daterange' => $rule['daterange'],
          'messages' => array('daterange' => $rule['messages']['daterange'])
      );
      unset($rule['daterange']);
      unset($rule['messages']['daterange']);
      if (empty($rule['messages'])) {
        unset($rule['messages']);
      }
    }
    if (isset($rule['datemin'])) {
      $settings['clientsideValidation']['forms'][$form['#id']]['dateminrules'][$key] = array(
          'daterange' => $rule['datemin'],
          'messages' => array('datemin' => $rule['messages']['datemin'])
      );
      unset($rule['datemin']);
      unset($rule['messages']['datemin']);
      if (empty($rule['messages'])) {
        unset($rule['messages']);
      }
    }
    if (isset($rule['datemax'])) {
      $settings['clientsideValidation']['forms'][$form['#id']]['datemaxrules'][$key] = array(
          'daterange' => $rule['datemax'],
          'messages' => array('datemax' => $rule['messages']['datemax'])
      );
      unset($rule['datemax']);
      unset($rule['messages']['datemax']);
      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
226
/**
227
 * Regular form.
Peter Droogmans's avatar
Peter Droogmans committed
228
229
230
 */
function clientside_validation_form_after_build(&$form, &$form_state) {
  static $js_rules = array();
231
232
233
234
  static $form_ids = array();
  if (isset($form_ids[$form['#id']])) {
    return $form;
  }
Jelle Sebreghts's avatar
Jelle Sebreghts committed
235

236
  drupal_alter("clientside_validation_form", $form, $form_state, $js_rules);
Peter Droogmans's avatar
Peter Droogmans committed
237
  if (!empty($js_rules)) {
238
    $settings = array();
239
    _clientside_validation_add_general_settings($settings, $form['#id']);
240
    _clientside_validation_add_special_rules($js_rules, $settings, $form);
Peter Droogmans's avatar
Peter Droogmans committed
241
242
243
244
245
246
    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');
    }
247
    //add our js file to the footer to support vertical_tabs
248
    drupal_add_js(drupal_get_path('module', 'clientside_validation') . '/clientside_validation.js', array('scope' => 'footer', 'weight' => 20));
Peter Droogmans's avatar
Peter Droogmans committed
249
    drupal_add_js($settings, 'setting');
250
    $form_ids[$form['#id']] = TRUE;
Peter Droogmans's avatar
Peter Droogmans committed
251
252
  }

253
  return $form;
Peter Droogmans's avatar
Peter Droogmans committed
254
255
}

256
257
258
259
260
261
262
263
264
265
266
267
268
function _clientside_validation_add_general_settings(&$settings, $form_id) {
  // 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(str_replace('-', '_', $form_id), $include_hidden)) {
    $setting['clientsideValidation']['forms'][$form_id]['includeHidden'] = TRUE;
  }

  $jquery = preg_split('/(\r\n?|\n)/', variable_get('clientside_validation_error_placement_jquery', ''), -1, PREG_SPLIT_NO_EMPTY);
  $top_of_form = preg_split('/(\r\n?|\n)/', variable_get('clientside_validation_error_placement_top_of_form', ''), -1, PREG_SPLIT_NO_EMPTY);
  $before_label = preg_split('/(\r\n?|\n)/', variable_get('clientside_validation_error_placement_before_label', ''), -1, PREG_SPLIT_NO_EMPTY);
  $after_label = preg_split('/(\r\n?|\n)/', variable_get('clientside_validation_error_placement_after_label', ''), -1, PREG_SPLIT_NO_EMPTY);
  $before_input = preg_split('/(\r\n?|\n)/', variable_get('clientside_validation_error_placement_before_input', ''), -1, PREG_SPLIT_NO_EMPTY);
269
  $custom_function = preg_split('/(\r\n?|\n)/', variable_get('clientside_validation_error_placement_custom_function', ''), -1, PREG_SPLIT_NO_EMPTY);
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
  $after_input = preg_split('/(\r\n?|\n)/', variable_get('clientside_validation_error_placement_after_input', ''), -1, PREG_SPLIT_NO_EMPTY);
  $jquery_selector = variable_get('clientside_validation_jquery_selector', '');

  if (in_array(str_replace('-', '_', $form_id), $jquery) && !empty($jquery_selector)) {
    $settings['clientsideValidation']['forms'][$form_id]['errorPlacement'] = CLIENTSIDE_VALIDATION_JQUERY_SELECTOR;
    $settings['clientsideValidation']['forms'][$form_id]['errorJquerySelector'] = $jquery_selector;
  }
  elseif (in_array(str_replace('-', '_', $form_id), $top_of_form)) {
    $settings['clientsideValidation']['forms'][$form_id]['errorPlacement'] = CLIENTSIDE_VALIDATION_TOP_OF_FORM;
  }
  elseif (in_array(str_replace('-', '_', $form_id), $before_label)) {
    $settings['clientsideValidation']['forms'][$form_id]['errorPlacement'] = CLIENTSIDE_VALIDATION_BEFORE_LABEL;
  }
  elseif (in_array(str_replace('-', '_', $form_id), $after_label)) {
    $settings['clientsideValidation']['forms'][$form_id]['errorPlacement'] = CLIENTSIDE_VALIDATION_AFTER_LABEL;
  }
  elseif (in_array(str_replace('-', '_', $form_id), $before_input)) {
    $settings['clientsideValidation']['forms'][$form_id]['errorPlacement'] = CLIENTSIDE_VALIDATION_BEFORE_INPUT;
  }
  elseif (in_array(str_replace('-', '_', $form_id), $after_input)) {
    $settings['clientsideValidation']['forms'][$form_id]['errorPlacement'] = CLIENTSIDE_VALIDATION_AFTER_INPUT;
  }
292
293
294
295
  elseif (in_array(str_replace('-', '_', $form_id), $custom_function)) {
    $settings['clientsideValidation']['forms'][$form_id]['errorPlacement'] = CLIENTSIDE_VALIDATION_CUSTOM_ERROR_FUNCTION;
    $settings['clientsideValidation']['forms'][$form_id]['customErrorFunction'] = variable_get('clientside_validation_custom_error_function', '');
  }
296
297
298
299
300
301
302
303
  else {
    if (variable_get('clientside_validation_error_placement_default', CLIENTSIDE_VALIDATION_TOP_OF_FIRST_FORM) == CLIENTSIDE_VALIDATION_JQUERY_SELECTOR
        && empty($jquery_selector)) {
      $settings['clientsideValidation']['forms'][$form_id]['errorPlacement'] = CLIENTSIDE_VALIDATION_TOP_OF_FIRST_FORM;
    }
    else {
      $settings['clientsideValidation']['forms'][$form_id]['errorPlacement'] = variable_get('clientside_validation_error_placement_default', CLIENTSIDE_VALIDATION_TOP_OF_FIRST_FORM);
      $settings['clientsideValidation']['forms'][$form_id]['errorJquerySelector'] = $jquery_selector;
304
      $settings['clientsideValidation']['forms'][$form_id]['customErrorFunction'] = variable_get('clientside_validation_custom_error_function', '');
305
306
307
    }
  }
  $form_settings_added[] = str_replace('-', '_', $form_id);
308
309
310
311
312
313
314
315
  $settings['clientsideValidation']['general'] = array(
    "errorClass" => "error",
    "wrapper" => "li",
    "validateTabs" =>  variable_get('clientside_validation_validate_tabs', 1),
    "scrollTo" => variable_get('clientside_validation_scrollto_errormessage', 1),
    "scrollSpeed" => (int)variable_get('clientside_validation_scroll_speed', 1000),
    "disableHtml5Validation" => variable_get('clientside_validation_disable_html5', 1),
    "validateOnBlur" => variable_get('clientside_validation_validate_onblur', 1),
316
    "validateOnBlurAlways" => (int)(variable_get('clientside_validation_validate_onblur_always', 0) && variable_get('clientside_validation_validate_onblur', 1)),
317
318
    "validateOnKeyUp" => variable_get('clientside_validation_validate_onkeyup', 1),
    "validateOnSubmit" => variable_get('clientside_validation_validate_onsubmit', 1),
319
    "errorElement" => variable_get('clientside_validation_error_element', 'label'),
320
    "showMessages" => variable_get('clientside_validation_show_messages', 0),
321
322
323
  );
}

324
325
326
/**
 * Set validation rule for required fields.
 */
Jelle's avatar
Jelle committed
327
function _clientside_validation_set_required($name, $title, $required, &$js_rules, $message = '') {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
328
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
Peter Droogmans's avatar
Peter Droogmans committed
329
330
  if ($required) {
    $js_rules[$name]['required'] = TRUE;
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
    if (empty($message)) {
      $variables = array(
        'message' => '!title field is required.',
        'placeholders' => array('!title' => $title),
        'error_type' => 'required',
        'element_name' => $name,
      );
    }
    else {
      $variables = array(
        'message' => $message,
        'error_type' => 'required',
        'element_name' => $name,
      );
    }
    $js_rules[$name]['messages']['required'] = theme('clientside_error', $variables);
Peter Droogmans's avatar
Peter Droogmans committed
347
348
349
  }
}

350
351
352
/**
 * Set validation rule for number fields.
 */
353
function _clientside_validation_set_number($name, $title, &$js_rules, $message = '') {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
354
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
Peter Droogmans's avatar
Peter Droogmans committed
355
  $js_rules[$name]['digits_negative'] = TRUE;
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
  if (empty($message)) {
    $variables = array(
      'message' => '!title field accepts only numbers.',
      'placeholders' => array('!title' => $title),
      'error_type' => 'number',
      'element_name' => $name,
    );
  }
  else {
    $variables = array(
      'message' => $message,
      'error_type' => 'number',
      'element_name' => $name,
    );
  }
371
  $js_rules[$name]['messages']['digits_negative'] = theme('clientside_error', $variables);
Peter Droogmans's avatar
Peter Droogmans committed
372
373
}

374
375
376
/**
 * Set validation rule for decimal fields.
 */
377
function _clientside_validation_set_number_decimal($name, $title, $decimalpoint, &$js_rules, $message = '') {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
378
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
Peter Droogmans's avatar
Peter Droogmans committed
379
380
  if ($decimalpoint == '.') {
    $js_rules[$name]['number'] = TRUE;
381
    $variables = array(
382
383
      'message' => empty($message) ? '!title field accepts only numbers (use a \'.\' as decimal point).' : $message,
      'placeholders' => empty($message) ? array('!title' => $title) : array(),
384
385
386
387
      'error_type' => 'number',
      'element_name' => $name,
    );
    $js_rules[$name]['messages']['number'] = theme('clientside_error', $variables);
Peter Droogmans's avatar
Peter Droogmans committed
388
389
390
  }
  else {
    $js_rules[$name]['numberDE'] = TRUE;
391
    $variables = array(
392
393
      'message' => empty($message) ? '!title field accepts only numbers (use a \',\' as decimal point).' : $message,
      'placeholers' => empty($message) ? array('!title' => $title) : array(),
394
395
396
397
      'error_type' => 'number',
      'element_name' => $name,
    );
    $js_rules[$name]['messages']['numberDE'] = theme('clientside_error', $variables);
Peter Droogmans's avatar
Peter Droogmans committed
398
399
400
  }
}

401
402
403
/**
 * Set validation rule for fields with a minimum and/or a maximum value.
 */
Jelle's avatar
Jelle committed
404
function _clientside_validation_set_minmax($name, $title, $min, $max, &$js_rules, $message = '') {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
405
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
406
  if (isset($min) && $min !== '' && isset($max) && $max !== '') {
Peter Droogmans's avatar
Peter Droogmans committed
407
    $js_rules[$name]['range'] = array($min, $max);
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
    if (empty($message)) {
      $variables = array(
        'message' => '!title field has to be between !min and !max.',
        'placeholders' => array('!title' => $title, '!min' => $min, '!max' => $max),
        'error_type' => 'range',
        'element_name' => $name,
      );
    }
    else {
      $variables = array(
        'message' => $message,
        'error_type' => 'range',
        'element_name' => $name,
      );
    }
    $js_rules[$name]['messages']['range'] = theme('clientside_error', $variables);
Peter Droogmans's avatar
Peter Droogmans committed
424
  }
425
  elseif (isset($min) && $min !== '') {
Peter Droogmans's avatar
Peter Droogmans committed
426
    $js_rules[$name]['min'] = $min;
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
    if (empty($message)) {
      $variables = array(
        'message' => '!title field has to be greater than !min.',
        'placeholders' => array('!title' => $title, '!min' => $min),
        'error_type' => 'min',
        'element_name' => $name,
      );
    }
    else {
      $variables = array(
        'message' => $message,
        'error_type' => 'min',
        'element_name' => $name,
      );
    }
    $js_rules[$name]['messages']['min'] = theme('clientside_error', $variables);
Peter Droogmans's avatar
Peter Droogmans committed
443
  }
444
  elseif (isset($max) && $max !== '') {
Peter Droogmans's avatar
Peter Droogmans committed
445
    $js_rules[$name]['max'] = $max;
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
    if (empty($message)) {
      $variables = array(
        'message' => '!title field has to be smaller than !max.',
        'placeholders' => array('!title' => $title, '!max' => $max),
        'error_type' => 'max',
        'element_name' => $name,
      );
    }
    else {
      $variables = array(
        'message' => $message,
        'error_type' => 'max',
        'element_name' => $name,
      );
    }
    $js_rules[$name]['messages']['max'] = theme('clientside_error', $variables);
Peter Droogmans's avatar
Peter Droogmans committed
462
463
464
  }
}

465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
function _clientside_validation_set_minmax_date($name, $title, $start_date, $end_date, &$js_rules, $message = '') {
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
  if (isset($start_date) && $start_date !== '' && isset($end_date) && $end_date !== '') {
    $js_rules[$name]['daterange'] = array($start_date, $end_date);
    if (empty($message)) {
      $variables = array(
        'message' => '!title field has to be between !min and !max.',
        'placeholders' => array('!title' => $title, '!min' => $start_date, '!max' => $end_date),
        'error_type' => 'daterange',
        'element_name' => $name,
      );
    }
    else {
      $variables = array(
        'message' => $message,
        'error_type' => 'daterange',
        'element_name' => $name,
      );
    }
    $js_rules[$name]['messages']['daterange'] = theme('clientside_error', $variables);
  }
  elseif (isset($start_date) && $start_date !== '') {
    $js_rules[$name]['datemin'] = $start_date;
    if (empty($message)) {
      $variables = array(
        'message' => '!title field has to be greater than !min.',
        'placeholders' => array('!title' => $title, '!min' => $start_date),
        'error_type' => 'datemin',
        'element_name' => $name,
      );
    }
    else {
      $variables = array(
        'message' => $message,
        'error_type' => 'datemin',
        'element_name' => $name,
      );
    }
    $js_rules[$name]['messages']['datemin'] = theme('clientside_error', $variables);
  }
  elseif (isset($end_date) && $end_date !== '') {
    $js_rules[$name]['datemax'] = $end_date;
    if (empty($message)) {
      $variables = array(
        'message' => '!title field has to be smaller than !max.',
        'placeholders' => array('!title' => $title, '!max' => $end_date),
        'error_type' => 'datemax',
        'element_name' => $name,
      );
    }
    else {
      $variables = array(
        'message' => $message,
        'error_type' => 'datemax',
        'element_name' => $name,
      );
    }
    $js_rules[$name]['messages']['datemax'] = theme('clientside_error', $variables);
  }
}

526
527
528
/**
 * Set validation rule for fields with a minimum and/or maximum length.
 */
Jelle's avatar
Jelle committed
529
function _clientside_validation_set_minmaxlength($name, $title, $min, $max, &$js_rules, $message = '') {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
530
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
Jelle Sebreghts's avatar
Jelle Sebreghts committed
531
  if ($max == 1) {
532
533
    $max = '';
  }
534
  if (isset($min) && $min !== '' && isset($max) && $max !== '') {
Peter Droogmans's avatar
Peter Droogmans committed
535
    $js_rules[$name]['rangelength'] = array($min, $max);
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
    if (empty($message)) {
      $variables = array(
        'message' => '!title field has to have between !min and !max values.',
        'placeholders' => array('!title' => $title, '!min' => $min, '!max' => $max),
        'error_type' => 'rangelength',
        'element_name' => $name,
      );
    }
    else {
      $variables = array(
        'message' => $message,
        'error_type' => 'rangelength',
        'element_name' => $name,
      );
    }
    $js_rules[$name]['messages']['rangelength'] = theme('clientside_error', $variables);
Peter Droogmans's avatar
Peter Droogmans committed
552
  }
553
  elseif (isset($min) && $min !== '') {
Peter Droogmans's avatar
Peter Droogmans committed
554
    $js_rules[$name]['minlength'] = $min;
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
    if (empty($message)) {
      $variables = array(
        'message' => '!title field has to have minimal !min values.',
        'placeholders' => array('!title' => $title, '!min' => $min),
        'error_type' => 'minlength',
        'element_name' => $name,
      );
    }
    else {
      $variables = array(
        'message' => $message,
        'error_type' => 'minlength',
        'element_name' => $name,
      );
    }
    $js_rules[$name]['messages']['minlength'] = theme('clientside_error', $variables);
Peter Droogmans's avatar
Peter Droogmans committed
571
  }
572
  elseif (isset($max) && $max !== '') {
Peter Droogmans's avatar
Peter Droogmans committed
573
    $js_rules[$name]['maxlength'] = $max;
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
    if (empty($message)) {
      $variables = array(
        'message' => '!title field has to have maximum !max values.',
        'placeholders' => array('!title' => $title, '!max' => $max),
        'error_type' => 'maxlength',
        'element_name' => $name,
      );
    }
    else {
      $variables = array(
        'message' => $message,
        'error_type' => 'maxlength',
        'element_name' => $name,
      );
    }
    $js_rules[$name]['messages']['maxlength'] = theme('clientside_error', $variables);
Peter Droogmans's avatar
Peter Droogmans committed
590
591
592
  }
}

593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
/**
 * Set validation rule for fields with a minimum and/or maximum amount of words.
 */
function _clientside_validation_set_minmax_words($name, $title, $min, $max, &$js_rules, $message = '') {
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
  if (isset($min) && $min !== '' && isset($max) && $max !== '') {
    $js_rules[$name]['rangewords'] = array($min, $max);
    if (empty($message)) {
      $variables = array(
        'message' => '!title field has to have between !min and !max words.',
        'placeholders' => array('!title' => $title, '!min' => $min, '!max' => $max),
        'error_type' => 'rangewords',
        'element_name' => $name,
      );
    }
    else {
      $variables = array(
        'message' => $message,
        'error_type' => 'rangewords',
        'element_name' => $name,
      );
    }
    $js_rules[$name]['messages']['rangewords'] = theme('clientside_error', $variables);
  }
  elseif (isset($min) && $min !== '') {
    $js_rules[$name]['minwords'] = $min;
    if (empty($message)) {
      $variables = array(
        'message' => '!title field has to have minimal !min words.',
        'placeholders' => array('!title' => $title, '!min' => $min),
        'error_type' => 'minwords',
        'element_name' => $name,
      );
    }
    else {
      $variables = array(
        'message' => $message,
        'error_type' => 'minwords',
        'element_name' => $name,
      );
    }
    $js_rules[$name]['messages']['minwords'] = theme('clientside_error', $variables);
  }
  elseif (isset($max) && $max !== '') {
    $js_rules[$name]['maxwords'] = $max;
    if (empty($message)) {
      $variables = array(
        'message' => '!title field has to have maximum !max words.',
        'placeholders' => array('!title' => $title, '!max' => $max),
        'error_type' => 'maxwords',
        'element_name' => $name,
      );
    }
    else {
      $variables = array(
        'message' => $message,
        'error_type' => 'maxwords',
        'element_name' => $name,
      );
    }
    $js_rules[$name]['messages']['maxwords'] = theme('clientside_error', $variables);
  }
}

/**
 * Set validation rule for plain text fields
 */
function _clientside_validation_set_plain_text($name, $title, $allowed_tags, &$js_rules, $message = '') {
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
  $js_rules[$name]['plaintext'] = empty($allowed_tags) ? '' : $allowed_tags;
  if (empty($message)) {
      $variables = array(
        'message' => (empty($allowed_tags)) ? '!title field can not contain any HTML tags' :  '!title field can not contain any HTML tags exept !allowed',
        'placeholders' => array('!title' => $title, '!allowed' => $allowed_tags),
        'error_type' => 'plaintext',
        'element_name' => $name,
      );
    }
    else {
      $variables = array(
        'message' => $message,
        'error_type' => 'plaintext',
        'element_name' => $name,
      );
    }
    $js_rules[$name]['messages']['plaintext'] = theme('clientside_error', $variables);
}

681
682
683
/**
 * Set validation rule for required fields that must equal a value from an other field.
 */
Jelle's avatar
Jelle committed
684
function _clientside_validation_set_equal($name, $title, $value, &$js_rules) {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
685
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
686
  $js_rules[$name]['equalTo'] = ':input[name=\'submitted[' . $value['form_key'] . ']\']';
687
688
689
690
691
692
693
  $variables = array(
    'message' => '!title field has to be equal to !firstone.',
    'placeholders' => array('!title' => $title, '!firstone' => $value['name']),
    'error_type' => 'equalto',
    'element_name' => $name,
  );
  $js_rules[$name]['messages']['equalTo'] = theme('clientside_error', $variables);
694
695
696
697
698
}

/**
 * Set validation rule for fields that can not be equal to a value from an other field.
 */
699
700
701
702
703
704
705
function _clientside_validation_set_not_equal($name, $title, $element, &$js_rules, $message = '', $webform = FALSE) {
  if ($webform) {
    $js_rules[$name]['notEqualTo'] = ':input[name=\'submitted[' . $element['form_key'] . ']\']';
  }
  else {
    $js_rules[$name]['notEqualTo'] = ':input[name=\'' . $element['form_key'] . '\']';
  }
Jelle Sebreghts's avatar
Jelle Sebreghts committed
706
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
707
708
709
  if (empty($message)) {
    $variables = array(
      'message' => '!title field has to different from !firstone.',
710
      'placeholders' => array('!title' => $title, '!firstone' => $element['name']),
711
712
713
714
715
716
717
718
719
720
721
722
      'error_type' => 'notequalto',
      'element_name' => $name,
    );
  }
  else {
    $variables = array(
      'message' => $message,
      'error_type' => 'notequalto',
      'element_name' => $name,
    );
  }
  $message = theme('clientside_error', $variables);
723
724
725
726
727
728
  $js_rules[$name]['messages']['notEqualTo'] = $message;
}

/**
 * Set validation rule for fields that must be equal to a specific value.
 */
Jelle's avatar
Jelle committed
729
function _clientside_validation_set_specific_value($name, $title, $value, &$js_rules, $message = '') {
730
  $js_rules[$name]['oneOf'] = $value;
Jelle Sebreghts's avatar
Jelle Sebreghts committed
731
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
732
733
  if (empty($message)) {
    $variables = array(
734
735
      'message' => '!title field has to be one of the following values: !values.',
      'placeholders' => array('!title' => $title, '!values' => implode(', ', $value)),
736
737
738
739
740
741
742
743
744
745
746
747
      'error_type' => 'specific value',
      'element_name' => $name
    );
  }
  else{
    $variables = array(
      'message' => $message,
      'error_type' => 'specific value',
      'element_name' => $name
    );
  }
  $message = theme('clientside_error', $variables);
748
749
750
  $js_rules[$name]['messages']['oneOf'] = $message;
}

751
752
753
754
755
756
/**
 * 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', '');
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
  if (empty($message)) {
    $variables = array(
      'message' => '!title field must consist of following elements only: !elements.',
      'placeholders' => array('!title' => $title, '!elements' => implode(', ', $values)),
      'error_type' => 'specific values',
      'element_name' => $name
    );
  }
  else {
    $variables = array(
      'message' => $message,
      'error_type' => 'specific values',
      'element_name' => $name
    );
  }
  $message = theme('clientside_error', $variables);
773
774
775
  $js_rules[$name]['messages']['specificVals'] = $message;
}

776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
/**
 * Set validation rule for fields that can not consist of one or more specific values
 */
function _clientside_validation_set_blacklist($name, $title, $values, &$js_rules, $message='') {
  $js_rules[$name]['blacklist'] = $values;
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
  if (empty($message)) {
    $variables = array(
      'message' => '!title field can not consist of following elements: !elements.',
      'placeholders' => array('!title' => $title, '!elements' => implode(', ', $values)),
      'error_type' => 'blacklist',
      'element_name' => $name
    );
  }
  else {
    $variables = array(
      'message' => $message,
      'error_type' => 'blacklist',
      'element_name' => $name
    );
  }
  $message = theme('clientside_error', $variables);
  $js_rules[$name]['messages']['blacklist'] = $message;
}

801
802
803
/**
 * Set validation rule for ean number fields.
 */
Jelle's avatar
Jelle committed
804
function _clientside_validation_set_ean($name, $title, &$js_rules, $message = '') {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
805
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
806
807
808
809
810
811
  if (empty($message)) {
    $variables = array(
      'message' => '!title field is not a valid EAN number.',
      'placeholders' => array('!title' => $title),
      'error_type' => 'ean',
      'element_name' => $name
Jelle Sebreghts's avatar
Jelle Sebreghts committed
812
    );
813
814
815
816
817
818
819
820
821
  }
  else {
    $variables = array(
      'message' => $message,
      'error_type' => 'ean',
      'element_name' => $name
    );
  }
  $message = theme('clientside_error', $variables);
822
  $js_rules[$name]['validEAN'] = TRUE;
Peter Droogmans's avatar
Peter Droogmans committed
823
  $js_rules[$name]['messages']['validEAN'] = $message;
Peter Droogmans's avatar
Peter Droogmans committed
824
825
}

826
827
828
/**
 * Set validation rule for fields with regex validation from webform_validation
 */
829
function _clientside_validation_set_regex($name, $title, &$js_rules, $expression, $message = '', $modifiers = "", $type = 'regex') {
830
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
Jelle Sebreghts's avatar
Jelle Sebreghts committed
831
  if (empty($message)) {
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
    $variables = array(
      'message' => '!title field does not match the required pattern.',
      'placeholders' => array('!title' => $title),
      'error_type' => $type,
      'element_name' => $name
    );
  }
  else {
    $variables = array(
      'message' => $message,
      'error_type' => $type,
      'element_name' => $name
    );
  }
  $message = theme('clientside_error', $variables);
847
  $js_rules[$name]['regexMatch'] = array($expression);
Jelle Sebreghts's avatar
Jelle Sebreghts committed
848
  if (!empty($modifiers)) {
849
850
    $js_rules[$name]['regexMatch'][] = $modifiers;
  }
851
852
853
  $js_rules[$name]['messages']['regexMatch'] = $message;
}

Jelle Sebreghts's avatar
Jelle Sebreghts committed
854
855
856
/**
 * Set validation rule for fields with regex validation from webform_validation
 */
857
function _clientside_validation_set_regex_pcre($name, $title, &$js_rules, $expression, $message = '', $type = 'regex pcre') {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
858
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
  if (empty($message)) {
    $variables = array(
      'message' => '!title field does not match the required pattern.',
      'placeholders' => array('!title' => $title),
      'error_type' => $type,
      'element_name' => $name
    );
  }
  else {
    $variables = array(
      'message' => $message,
      'error_type' => $type,
      'element_name' => $name
    );
  }
  $message = theme('clientside_error', $variables);
Jelle Sebreghts's avatar
Jelle Sebreghts committed
875
  $js_rules[$name]['regexMatchPCRE']['expressions'][] = $expression;
876
  $js_rules[$name]['regexMatchPCRE']['messages'][] = $message;
Jelle Sebreghts's avatar
Jelle Sebreghts committed
877
878
}

879
880
881
/**
 * Set validation rule for file fields that must have a certain extension.
 */
Jelle's avatar
Jelle committed
882
function _clientside_validation_set_extensions($name, $extensions, &$js_rules) {
883
884
  $extension_list = preg_replace('#,(?![^,]+,)#', ' or', implode(', ', $extensions));
  $js_rules[$name]['accept'] = implode('|', $extensions);
885
886
887
888
889
890
891
  $variables = array(
    'message' => "Only files with a %exts extension are allowed.",
    'placeholders' => array('%exts' => $extension_list),
    'error_type' => 'extensions',
    'element_name' => $name
  );
  $js_rules[$name]['messages']['accept'] = theme('clientside_error', $variables);
892
893
894
895
896
}

/**
 * Set validation rule for checkboxes.
 */
897
function _clientside_validation_set_checkboxgroup_minmax($name, $title, $id, &$js_rules, $message = '', $min = 1, $max = 99) {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
898
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
899
  $js_rules[$name]['checkboxgroupminmax'] = array($min, $max, $id);
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
  if ($message == '') {
    if ($min == 1 && $max == 99) {
      $message = '!title field is required.';
      $placeholders = array('!title' => $title);
    }
    if ($min == 0 && $max != 99) {
      $message = 'You can select no more than !max values for !title.';
      $placeholders = array('!title' => $title, '!max' => $max);
    }
    if ($min != 1 && $max == 99) {
      $message = 'You must select at least !min values for !title.';
      $placeholders = array('!title' => $title, '!min' => $min);
    }
    if ($min > 0 && $max != 99) {
      $message = 'You must select between !min and !max values for !title.';
      $placeholders = array('!title' => $title, '!min' => $min, '!max' => $max);
    }
  }
  else {
    $placeholders = array();
  }
  $variables = array(
    'message' => $message,
    'placeholders' => $placeholders,
924
925
926
927
    'error_type' => 'required',
    'element_name' => $name
  );
  $js_rules[$name]['messages']['checkboxgroupminmax'] = theme('clientside_error', $variables);
928
929
930
931
932
}

/**
 * Set validation rule for email fields.
 */
933
function _clientside_validation_set_email($name, $title, &$js_rules, $message = '') {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
934
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
935
  $js_rules[$name]['email'] = TRUE;
936
  $variables = array(
937
938
    'message' => empty($message) ? 'The value in !title is not a valid email address.' : $message,
    'placeholders' => empty($message) ? array('!title' => $title) : array(),
939
940
941
942
    'error_type' => 'email',
    'element_name' => $name
  );
  $js_rules[$name]['messages']['email'] = theme('clientside_error', $variables);
943
944
}

945
946
947
/**
 * Set validation rule for url fields.
 */
948
function _clientside_validation_set_url($name, $title, &$js_rules, $message = '') {
949
950
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
  $js_rules[$name]['url'] = TRUE;
951
  $variables = array(
952
953
    'message' => empty($message) ? 'The value in !title is not a valid url.' : $message,
    'placeholders' => empty($message) ? array('!title' => $title) : array(),
954
955
956
957
    'error_type' => 'url',
    'element_name' => $name
  );
  $js_rules[$name]['messages']['url'] = theme('clientside_error', $variables);
958
959
}

960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
/**
 * Set validation rule for "require at least one of several".
 */
function _clientside_validation_set_require_oneof($names, $titles, $message = "", &$js_rules) {
  $original_message = $message;
  foreach ($names as $key => $name) {
    $temp_titles = $titles;
    unset($temp_titles[$key]);
    if (empty ($original_message)) {
      $string_titles = '<ul><li>' . implode('</li><li>', $temp_titles) . '</li></ul>';
      $message = t('!name field is required unless you specify at least one of these items: !items', array('!name' => $titles[$key], '!items' => $string_titles));
    }
    $js_rules[$name]['requireOneOf'] = array_diff($names, array($name));
    $variables = array(
      'message' => $message,
      'error_type' => 'requireOneOf',
      'element_name' => $name,
    );
    $js_rules[$name]['messages']['requireOneOf'] = theme('clientside_error', $variables);
  }
}

982
983
984
985
986
987
988
989
990
991
992
993
function _clientside_validation_set_phone($name, $title, $country_code, &$js_rules, $message = "") {
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
  $js_rules[$name]['phone'] = $country_code;
  $variables = array(
    'message' => empty($message) ? 'The value in !title is not a valid phone number.' : $message,
    'placeholders' => empty($message) ? array('!title' => $title) : array(),
    'error_type' => 'phone',
    'element_name' => $name
  );
  $js_rules[$name]['messages']['phone'] = theme('clientside_error', $variables);
}

994
995
function clientside_validation_drupal_json_encode($var) {
  return str_replace(array('<', '>', '&'), array('\u003c', '\u003e', '\u0026'), json_encode($var));
996
}
997
998
999
1000

/**
 * Implements hook_theme().
 */
For faster browsing, not all history is shown. View entire blame