clientside_validation.module 44.9 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
  $items['admin/config/validation'] = array(
    'title' => 'Validation',
    'description' => 'Validation settings.',
    'position' => 'right',
    'weight' => -10,
Peter Droogmans's avatar
Peter Droogmans committed
31
32
33
34
    'page callback' => 'system_admin_menu_block_page',
    'access arguments' => array('administer site configuration'),
    'file' => 'system.admin.inc',
    'file path' => drupal_get_path('module', 'system'),
35
36
  );
  $items['admin/config/validation/clientside_validation'] = array(
Peter Droogmans's avatar
Peter Droogmans committed
37
38
    'title' => 'Clientside Validation',
    'description' => 'Administer clientside validation.',
Peter Droogmans's avatar
Peter Droogmans committed
39
    'page callback' => 'system_admin_menu_block_page',
40
    'access arguments' => array('administer site configuration'),
Peter Droogmans's avatar
Peter Droogmans committed
41
42
    'file' => 'system.admin.inc',
    'file path' => drupal_get_path('module', 'system'),
43
44
  );
  $items['admin/config/validation/clientside_validation/general'] = array(
Peter Droogmans's avatar
Peter Droogmans committed
45
46
    'title' => 'General settings',
    'description' => 'Edit general settings.',
47
    'weight' => -1,
Peter Droogmans's avatar
Peter Droogmans committed
48
49
50
51
    'page callback' => 'drupal_get_form',
    'page arguments' => array('clientside_validation_general_settings_form'),
    'access arguments' => array('administer site configuration'),
    'file' => 'clientside_validation.admin.inc',
52
53
  );
  $items['admin/config/validation/clientside_validation/default'] = array(
Peter Droogmans's avatar
Peter Droogmans committed
54
55
    'title' => 'Default settings',
    'description' => 'Edit default settings.',
56
57
    'page callback' => 'drupal_get_form',
    'page arguments' => array('clientside_validation_settings_form'),
Peter Droogmans's avatar
Peter Droogmans committed
58
    'access arguments' => array('administer site configuration'),
59
    'file' => 'clientside_validation.admin.inc',
Peter Droogmans's avatar
Peter Droogmans committed
60
    'weight' => 5,
Jelle Sebreghts's avatar
Jelle Sebreghts committed
61
  );
Peter Droogmans's avatar
Peter Droogmans committed
62
63
  $items['admin/config/validation/clientside_validation/content-types'] = array(
    'title' => 'Content types',
Peter Droogmans's avatar
Peter Droogmans committed
64
    'description' => 'Override settings for content types.',
Peter Droogmans's avatar
Peter Droogmans committed
65
66
67
68
    'page callback' => 'drupal_get_form',
    'page arguments' => array('clientside_validation_settings_overview', 'content-types'),
    'access arguments' => array('administer site configuration'),
    'file' => 'clientside_validation.admin.inc',
Peter Droogmans's avatar
Peter Droogmans committed
69
    'weight' => 10,
Peter Droogmans's avatar
Peter Droogmans committed
70
  );
71
72
  $items['admin/config/validation/clientside_validation/webforms'] = array(
    'title' => 'Webforms',
Peter Droogmans's avatar
Peter Droogmans committed
73
    'description' => 'Override settings for webforms.',
74
75
76
77
78
79
80
81
    'page callback' => 'drupal_get_form',
    'page arguments' => array('clientside_validation_settings_overview', 'webforms'),
    'access arguments' => array('administer site configuration'),
    'file' => 'clientside_validation.admin.inc',
    'weight' => 11,
  );
  $items['admin/config/validation/clientside_validation/custom-forms'] = array(
    'title' => 'Custom forms',
Peter Droogmans's avatar
Peter Droogmans committed
82
    'description' => 'Override settings for custom forms.',
83
84
85
86
87
88
    'page callback' => 'drupal_get_form',
    'page arguments' => array('clientside_validation_settings_overview', 'custom-forms'),
    'access arguments' => array('administer site configuration'),
    'file' => 'clientside_validation.admin.inc',
    'weight' => 12,
  );
Peter Droogmans's avatar
Peter Droogmans committed
89
90
  $items['admin/config/validation/clientside_validation/%/%/edit'] = array(
    'title' => 'Edit settings',
Peter Droogmans's avatar
Peter Droogmans committed
91
    'type' => MENU_CALLBACK,
Peter Droogmans's avatar
Peter Droogmans committed
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
    'page callback' => 'drupal_get_form',
    'page arguments' => array('clientside_validation_settings_form', 5, 4),
    'access arguments' => array('administer site configuration'),
    'file' => 'clientside_validation.admin.inc',
  );
  $items['admin/config/validation/clientside_validation/%/%/disable'] = array(
    'title' => 'Disable settings',
    'type' => MENU_CALLBACK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('clientside_validation_settings_confirmation_form', 5, 4, 'disable'),
    'access arguments' => array('administer site configuration'),
    'file' => 'clientside_validation.admin.inc',
  );
  $items['admin/config/validation/clientside_validation/%/%/enable'] = array(
    'title' => 'Enable settings',
    'type' => MENU_CALLBACK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('clientside_validation_settings_confirmation_form', 5, 4, 'enable'),
    'access arguments' => array('administer site configuration'),
    'file' => 'clientside_validation.admin.inc',
  );
  $items['admin/config/validation/clientside_validation/%/%/delete'] = array(
    'title' => 'Delete settings',
    'type' => MENU_CALLBACK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('clientside_validation_settings_confirmation_form', 5, 4, 'delete'),
    'access arguments' => array('administer site configuration'),
    'file' => 'clientside_validation.admin.inc',
  );
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
121

122
  $items['clientside_validation/ajax'] = array(
Jelle Sebreghts's avatar
Jelle Sebreghts committed
123
124
125
126
    '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
127
  );
128
129
130
131
132
133
  $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
134
135
136
  return $items;
}

Jelle Sebreghts's avatar
Jelle Sebreghts committed
137
function _clientside_validation_ajax_call() {
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
138
139
  $param = check_plain($_POST['param']);
  $value = check_plain($_POST['value']);
Jelle Sebreghts's avatar
Jelle Sebreghts committed
140
141
142
  $result = array();
  foreach ($param['expressions'] as $key => $regex) {
    if (!(bool) preg_match($regex, (string) $value)) {
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
143
      $result = array('result' => FALSE, 'message' => $param['messages'][$key]);
Jelle Sebreghts's avatar
Jelle Sebreghts committed
144
145
146
147
148
149
150
151
152
      break;
    }
  }
  if (empty($result)) {
    $result = array('result' => TRUE);
  }
  drupal_json_output($result);
}

153
function _clientside_validation_ajax_phone() {
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
154
155
  $value = check_plain($_POST['value']);
  $country_code = check_plain($_POST['country_code']);
156
157
158
159
  $result['result'] = valid_phone_number($country_code, $value);
  drupal_json_output($result);
}

Peter Droogmans's avatar
Peter Droogmans committed
160
/**
Jelle Sebreghts's avatar
Jelle Sebreghts committed
161
 * Implements hook_form_alter().
Peter Droogmans's avatar
Peter Droogmans committed
162
 */
163
function clientside_validation_form_alter(&$form, &$form_state, $form_id) {
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
  $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
180
  if (!$page_match) {
181
182
    return;
  }
183
184
  $validate_specific_setting = variable_get('clientside_validation_validate_specific', '');
  $validate_specific = preg_split('/(\r\n?|\n)/', $validate_specific_setting, -1, PREG_SPLIT_NO_EMPTY);
185
186
187
188
189
190
  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)) {
191
192
    return;
  }
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
193

194
  if (strpos($form_id, 'webform_client_form') !== FALSE) {
Peter Droogmans's avatar
Peter Droogmans committed
195
    $definedsettings = clientside_validation_settings_load('webforms', $form['#node']->nid);
Peter Droogmans's avatar
Peter Droogmans committed
196
    clientside_validation_settings_current_form($definedsettings);
197
    if (isset($form['actions']['previous'])) {
198
      $form['actions']['previous']['#attributes']['class'][] = 'cancel';
199
    }
200
    $form['#after_build'][] = 'clientside_validation_webform_after_build';
201
  }
202
  else {
Peter Droogmans's avatar
Peter Droogmans committed
203
    if (isset($form['#node_edit_form']) && $form['#node_edit_form']) {
Peter Droogmans's avatar
Peter Droogmans committed
204
      $definedsettings = clientside_validation_settings_load('content-types', $form['#node']->type);
Peter Droogmans's avatar
Peter Droogmans committed
205
206
    }
    else {
Peter Droogmans's avatar
Peter Droogmans committed
207
      $definedsettings = clientside_validation_settings_load('content-types', $form_id);
Peter Droogmans's avatar
Peter Droogmans committed
208
209
    }
    clientside_validation_settings_current_form($definedsettings);
210
211
212
    if (isset($form['actions']['delete'])) {
      $form['actions']['delete']['#attributes']['class'][] = 'cancel';
    }
213
    $form['#after_build'][] = 'clientside_validation_form_after_build';
214
215
216
217
218
219
220
221
222
  }
}

/**
 * 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
223
  if (!empty($js_rules)) {
224
    $settings = array();
225
    _clientside_validation_add_general_settings($settings, $form['#id']);
226
    _clientside_validation_add_special_rules($js_rules, $settings, $form);
Jelle Sebreghts's avatar
Jelle Sebreghts committed
227

Peter Droogmans's avatar
Peter Droogmans committed
228
229
230
231
232
233
    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');
    }
234
    //add our js file to the footer to support vertical_tabs
235
    drupal_add_js(drupal_get_path('module', 'clientside_validation') . '/clientside_validation.js', array('scope' => 'footer', 'weight' => 20));
Peter Droogmans's avatar
Peter Droogmans committed
236
237
238
239
240
    drupal_add_js($settings, 'setting');
  }
  return $form;
}

241
function _clientside_validation_add_special_rules(&$js_rules, &$settings, $form) {
242
  if (isset($js_rules['_groups'])) {
243
    $settings['clientsideValidation']['groups'][$form['#id']] = (object)$js_rules['_groups'];
244
245
246
    unset($js_rules['_groups']);
  }
  else {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
247
    $settings['clientsideValidation']['groups'][$form['#id']] = new stdClass();
248
  }
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
  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
300
/**
301
 * Regular form.
Peter Droogmans's avatar
Peter Droogmans committed
302
303
304
 */
function clientside_validation_form_after_build(&$form, &$form_state) {
  static $js_rules = array();
305
  static $form_ids = array();
Peter Droogmans's avatar
Peter Droogmans committed
306

307
308
309
  if (isset($form_ids[$form['#id']])) {
    return $form;
  }
Jelle Sebreghts's avatar
Jelle Sebreghts committed
310

311
  drupal_alter("clientside_validation_form", $form, $form_state, $js_rules);
Peter Droogmans's avatar
Peter Droogmans committed
312
  if (!empty($js_rules)) {
313
    $settings = array();
314
    _clientside_validation_add_general_settings($settings, $form['#id']);
315
    _clientside_validation_add_special_rules($js_rules, $settings, $form);
Peter Droogmans's avatar
Peter Droogmans committed
316
317
318
319
320
321
    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');
    }
322
    //add our js file to the footer to support vertical_tabs
323
    drupal_add_js(drupal_get_path('module', 'clientside_validation') . '/clientside_validation.js', array('scope' => 'footer', 'weight' => 20));
Peter Droogmans's avatar
Peter Droogmans committed
324
    drupal_add_js($settings, 'setting');
325
    $form_ids[$form['#id']] = TRUE;
Peter Droogmans's avatar
Peter Droogmans committed
326
327
  }

328
  return $form;
Peter Droogmans's avatar
Peter Droogmans committed
329
330
}

331
function _clientside_validation_add_general_settings(&$settings, $form_id) {
Peter Droogmans's avatar
Peter Droogmans committed
332
333
  $definedsettings = clientside_validation_settings_current_form();

334
  // Add a setting for this form if the hidden elements should still be validated
Peter Droogmans's avatar
Peter Droogmans committed
335
  $include_hidden_setting = $definedsettings['include_hidden']['include_hidden'];
336
337
338
339
  $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;
  }
Peter Droogmans's avatar
Peter Droogmans committed
340
341
  elseif ($definedsettings['include_hidden']['include_hidden']) {
    $setting['clientsideValidation']['forms'][$form_id]['includeHidden'] = TRUE;
342
  }
Peter Droogmans's avatar
Peter Droogmans committed
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361

  $settings['clientsideValidation']['forms'][$form_id]['errorPlacement'] = $definedsettings['error_placement']['error_placement_default'];
  switch ($definedsettings['error_placement']['error_placement_default']) {
    case CLIENTSIDE_VALIDATION_JQUERY_SELECTOR:
      if (!empty($definedsettings['error_placement']['jquery_selector'])) {
        $settings['clientsideValidation']['forms'][$form_id]['errorJquerySelector'] = $definedsettings['error_placement']['jquery_selector'];
      }
      else {
        $settings['clientsideValidation']['forms'][$form_id]['errorPlacement'] = CLIENTSIDE_VALIDATION_TOP_OF_FIRST_FORM;
      }
      break;
    case CLIENTSIDE_VALIDATION_CUSTOM_ERROR_FUNCTION:
      if (!empty($definedsettings['error_placement']['custom_error_function'])) {
        $settings['clientsideValidation']['forms'][$form_id]['customErrorFunction'] = $definedsettings['error_placement']['custom_error_function'];
      }
      else {
        $settings['clientsideValidation']['forms'][$form_id]['errorPlacement'] = CLIENTSIDE_VALIDATION_TOP_OF_FIRST_FORM;
      }
      break;
362
  }
Peter Droogmans's avatar
Peter Droogmans committed
363

364
  $form_settings_added[] = str_replace('-', '_', $form_id);
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
365

Peter Droogmans's avatar
Peter Droogmans committed
366
367
368
369
370
371
372
373
374
375
376
377
378
  $settings['clientsideValidation']['forms'][$form_id]['general'] = array(
    "errorClass" => "error", // @TODO: option?
    "wrapper" => "li", // @TODO: option?
    "validateTabs" => $definedsettings['include_hidden']['validate_tabs'],
    "scrollTo" => $definedsettings['error']['scrollto_errormessage'],
    "scrollSpeed" => $definedsettings['error']['scroll_speed'],
    "disableHtml5Validation" => $definedsettings['validate_options']['disable_html5'],
    "validateOnBlur" => $definedsettings['validate_options']['validate_onblur'],
    "validateOnBlurAlways" => $definedsettings['validate_options']['validate_onblur_always'],
    "validateOnKeyUp" => $definedsettings['validate_options']['validate_onkeyup'],
    "validateOnSubmit" => $definedsettings['validate_options']['validate_onsubmit'],
    "showMessages" => $definedsettings['validate_options']['show_messages'],
    "errorElement" => $definedsettings['error']['error_element'],
379
380
381
  );
}

382
383
384
/**
 * Set validation rule for required fields.
 */
Jelle's avatar
Jelle committed
385
function _clientside_validation_set_required($name, $title, $required, &$js_rules, $message = '') {
Peter Droogmans's avatar
Peter Droogmans committed
386
  $title = _clientside_validation_set_title($title);
Peter Droogmans's avatar
Peter Droogmans committed
387
388
  if ($required) {
    $js_rules[$name]['required'] = TRUE;
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
    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
405
406
407
  }
}

Peter Droogmans's avatar
Peter Droogmans committed
408
409
410
411
412
413
414
415
/**
 * Set validation rule for number fields.
 */
function _clientside_validation_set_title($title) {
  $settings = clientside_validation_settings_current_form();
  return $settings['error']['prefix'] . $title . $settings['error']['suffix'];
}

416
417
418
/**
 * Set validation rule for number fields.
 */
419
function _clientside_validation_set_number($name, $title, &$js_rules, $message = '') {
Peter Droogmans's avatar
Peter Droogmans committed
420
  $title = _clientside_validation_set_title($title);
Peter Droogmans's avatar
Peter Droogmans committed
421
  $js_rules[$name]['digits_negative'] = TRUE;
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
  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,
    );
  }
437
  $js_rules[$name]['messages']['digits_negative'] = theme('clientside_error', $variables);
Peter Droogmans's avatar
Peter Droogmans committed
438
439
}

440
441
442
/**
 * Set validation rule for decimal fields.
 */
443
function _clientside_validation_set_number_decimal($name, $title, $decimalpoint, &$js_rules, $message = '') {
Peter Droogmans's avatar
Peter Droogmans committed
444
  $title = _clientside_validation_set_title($title);
Peter Droogmans's avatar
Peter Droogmans committed
445
446
  if ($decimalpoint == '.') {
    $js_rules[$name]['number'] = TRUE;
447
    $variables = array(
448
449
      'message' => empty($message) ? '!title field accepts only numbers (use a \'.\' as decimal point).' : $message,
      'placeholders' => empty($message) ? array('!title' => $title) : array(),
450
451
452
453
      'error_type' => 'number',
      'element_name' => $name,
    );
    $js_rules[$name]['messages']['number'] = theme('clientside_error', $variables);
Peter Droogmans's avatar
Peter Droogmans committed
454
455
456
  }
  else {
    $js_rules[$name]['numberDE'] = TRUE;
457
    $variables = array(
458
459
      'message' => empty($message) ? '!title field accepts only numbers (use a \',\' as decimal point).' : $message,
      'placeholers' => empty($message) ? array('!title' => $title) : array(),
460
461
462
463
      'error_type' => 'number',
      'element_name' => $name,
    );
    $js_rules[$name]['messages']['numberDE'] = theme('clientside_error', $variables);
Peter Droogmans's avatar
Peter Droogmans committed
464
465
466
  }
}

467
468
469
/**
 * Set validation rule for fields with a minimum and/or a maximum value.
 */
Jelle's avatar
Jelle committed
470
function _clientside_validation_set_minmax($name, $title, $min, $max, &$js_rules, $message = '') {
Peter Droogmans's avatar
Peter Droogmans committed
471
  $title = _clientside_validation_set_title($title);
472
  if (isset($min) && $min !== '' && isset($max) && $max !== '') {
Peter Droogmans's avatar
Peter Droogmans committed
473
    $js_rules[$name]['range'] = array($min, $max);
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
    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
490
  }
491
  elseif (isset($min) && $min !== '') {
Peter Droogmans's avatar
Peter Droogmans committed
492
    $js_rules[$name]['min'] = $min;
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
    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
509
  }
510
  elseif (isset($max) && $max !== '') {
Peter Droogmans's avatar
Peter Droogmans committed
511
    $js_rules[$name]['max'] = $max;
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
    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
528
529
530
  }
}

531
function _clientside_validation_set_minmax_date($name, $title, $start_date, $end_date, &$js_rules, $message = '') {
Peter Droogmans's avatar
Peter Droogmans committed
532
  $title = _clientside_validation_set_title($title);
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
  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);
  }
}

592
593
594
/**
 * Set validation rule for fields with a minimum and/or maximum length.
 */
Jelle's avatar
Jelle committed
595
function _clientside_validation_set_minmaxlength($name, $title, $min, $max, &$js_rules, $message = '') {
Peter Droogmans's avatar
Peter Droogmans committed
596
  $title = _clientside_validation_set_title($title);
Jelle Sebreghts's avatar
Jelle Sebreghts committed
597
  if ($max == 1) {
598
599
    $max = '';
  }
600
  if (isset($min) && $min !== '' && isset($max) && $max !== '') {
Peter Droogmans's avatar
Peter Droogmans committed
601
    $js_rules[$name]['rangelength'] = array($min, $max);
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
    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
618
  }
619
  elseif (isset($min) && $min !== '') {
Peter Droogmans's avatar
Peter Droogmans committed
620
    $js_rules[$name]['minlength'] = $min;
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
    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
637
  }
638
  elseif (isset($max) && $max !== '') {
Peter Droogmans's avatar
Peter Droogmans committed
639
    $js_rules[$name]['maxlength'] = $max;
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
    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
656
657
658
  }
}

659
660
661
662
/**
 * 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 = '') {
Peter Droogmans's avatar
Peter Droogmans committed
663
  $title = _clientside_validation_set_title($title);
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
  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 = '') {
Peter Droogmans's avatar
Peter Droogmans committed
727
  $title = _clientside_validation_set_title($title);
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
  $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);
}

747
748
749
/**
 * Set validation rule for required fields that must equal a value from an other field.
 */
Jelle's avatar
Jelle committed
750
function _clientside_validation_set_equal($name, $title, $value, &$js_rules) {
Peter Droogmans's avatar
Peter Droogmans committed
751
  $title = _clientside_validation_set_title($title);
752
  $js_rules[$name]['equalTo'] = ':input[name=\'submitted[' . $value['form_key'] . ']\']';
753
754
755
756
757
758
759
  $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);
760
761
762
763
764
}

/**
 * Set validation rule for fields that can not be equal to a value from an other field.
 */
765
function _clientside_validation_set_not_equal($name, $title, $element, &$js_rules, $message = '', $webform = FALSE) {
Peter Droogmans's avatar
Peter Droogmans committed
766
  $title = _clientside_validation_set_title($title);
767
768
769
770
771
772
  if ($webform) {
    $js_rules[$name]['notEqualTo'] = ':input[name=\'submitted[' . $element['form_key'] . ']\']';
  }
  else {
    $js_rules[$name]['notEqualTo'] = ':input[name=\'' . $element['form_key'] . '\']';
  }
773
774
775
  if (empty($message)) {
    $variables = array(
      'message' => '!title field has to different from !firstone.',
776
      'placeholders' => array('!title' => $title, '!firstone' => $element['name']),
777
778
779
780
781
782
783
784
785
786
787
788
      'error_type' => 'notequalto',
      'element_name' => $name,
    );
  }
  else {
    $variables = array(
      'message' => $message,
      'error_type' => 'notequalto',
      'element_name' => $name,
    );
  }
  $message = theme('clientside_error', $variables);
789
790
791
792
793
794
  $js_rules[$name]['messages']['notEqualTo'] = $message;
}

/**
 * Set validation rule for fields that must be equal to a specific value.
 */
Jelle's avatar
Jelle committed
795
function _clientside_validation_set_specific_value($name, $title, $value, &$js_rules, $message = '') {
796
  $js_rules[$name]['oneOf'] = $value;
Peter Droogmans's avatar
Peter Droogmans committed
797
  $title = _clientside_validation_set_title($title);
798
799
  if (empty($message)) {
    $variables = array(
800
801
      'message' => '!title field has to be one of the following values: !values.',
      'placeholders' => array('!title' => $title, '!values' => implode(', ', $value)),
802
803
804
805
806
807
808
809
810
811
812
813
      'error_type' => 'specific value',
      'element_name' => $name
    );
  }
  else{
    $variables = array(
      'message' => $message,
      'error_type' => 'specific value',
      'element_name' => $name
    );
  }
  $message = theme('clientside_error', $variables);
814
815
816
  $js_rules[$name]['messages']['oneOf'] = $message;
}

817
818
819
820
821
/**
 * 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;
Peter Droogmans's avatar
Peter Droogmans committed
822
  $title = _clientside_validation_set_title($title);
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
  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);
839
840
841
  $js_rules[$name]['messages']['specificVals'] = $message;
}

842
843
844
845
846
/**
 * 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;
Peter Droogmans's avatar
Peter Droogmans committed
847
  $title = _clientside_validation_set_title($title);
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
  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;
}

867
868
869
/**
 * Set validation rule for ean number fields.
 */
Jelle's avatar
Jelle committed
870
function _clientside_validation_set_ean($name, $title, &$js_rules, $message = '') {
Peter Droogmans's avatar
Peter Droogmans committed
871
  $title = _clientside_validation_set_title($title);
872
873
874
875
876
877
  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
878
    );
879
880
881
882
883
884
885
886
887
  }
  else {
    $variables = array(
      'message' => $message,
      'error_type' => 'ean',
      'element_name' => $name
    );
  }
  $message = theme('clientside_error', $variables);
888
  $js_rules[$name]['validEAN'] = TRUE;
Peter Droogmans's avatar
Peter Droogmans committed
889
  $js_rules[$name]['messages']['validEAN'] = $message;
Peter Droogmans's avatar
Peter Droogmans committed
890
891
}

892
893
894
/**
 * Set validation rule for fields with regex validation from webform_validation
 */
895
function _clientside_validation_set_regex($name, $title, &$js_rules, $expression, $message = '', $modifiers = "", $type = 'regex') {
Peter Droogmans's avatar
Peter Droogmans committed
896
  $title = _clientside_validation_set_title($title);
Jelle Sebreghts's avatar
Jelle Sebreghts committed
897
  if (empty($message)) {
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
    $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);
913
  $js_rules[$name]['regexMatch'] = array($expression);
Jelle Sebreghts's avatar
Jelle Sebreghts committed
914
  if (!empty($modifiers)) {
915
916
    $js_rules[$name]['regexMatch'][] = $modifiers;
  }
917
918
919
  $js_rules[$name]['messages']['regexMatch'] = $message;
}

Jelle Sebreghts's avatar
Jelle Sebreghts committed
920
921
922
/**
 * Set validation rule for fields with regex validation from webform_validation
 */
923
function _clientside_validation_set_regex_pcre($name, $title, &$js_rules, $expression, $message = '', $type = 'regex pcre') {
Peter Droogmans's avatar
Peter Droogmans committed
924
  $title = _clientside_validation_set_title($title);
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
  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
941
  $js_rules[$name]['regexMatchPCRE']['expressions'][] = $expression;
942
  $js_rules[$name]['regexMatchPCRE']['messages'][] = $message;
Jelle Sebreghts's avatar
Jelle Sebreghts committed
943
944
}

945
946
947
/**
 * Set validation rule for file fields that must have a certain extension.
 */
Jelle's avatar
Jelle committed
948
function _clientside_validation_set_extensions($name, $extensions, &$js_rules) {
949
950
  $extension_list = preg_replace('#,(?![^,]+,)#', ' or', implode(', ', $extensions));
  $js_rules[$name]['accept'] = implode('|', $extensions);
951
952
953
954
955
956
957
  $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);
958
959
960
961
962
}

/**
 * Set validation rule for checkboxes.
 */
963
function _clientside_validation_set_checkboxgroup_minmax($name, $title, $id, &$js_rules, $message = '', $min = 1, $max = 99) {
Peter Droogmans's avatar
Peter Droogmans committed
964
  $title = _clientside_validation_set_title($title);
965
  $js_rules[$name]['checkboxgroupminmax'] = array($min, $max, $id);
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
  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,
990
991
992
993
    'error_type' => 'required',
    'element_name' => $name
  );
  $js_rules[$name]['messages']['checkboxgroupminmax'] = theme('clientside_error', $variables);
994
995
996
997
998
}

/**
 * Set validation rule for email fields.
 */
999
function _clientside_validation_set_email($name, $title, &$js_rules, $message = '') {
Peter Droogmans's avatar
Peter Droogmans committed
1000
  $title = _clientside_validation_set_title($title);