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

define('CLIENTSIDE_VALIDATION_EXCLUDE_PATHS', 0); //Add validation on all paths except those specified
define('CLIENTSIDE_VALIDATION_INCLUDE_PATHS', 1); //Only add validation on specified paths
13
14
15
16
17
18
19
20
21

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);

Peter Droogmans's avatar
Peter Droogmans committed
22
23
24
25
/**
 * Implements hook_menu().
 */
function clientside_validation_menu() {
26
27
28
29
30
31
32
33
  $items['admin/config/validation'] = array(
    'title' => 'Validation',
    'description' => 'Validation settings.',
    'position' => 'right',
    'weight' => -10,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('clientside_validation_admin_settings'),
    'access arguments' => array('access administration pages'),
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
40
41
    'title' => 'Clientside Validation',
    'description' => 'Administer clientside validation.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('clientside_validation_admin_settings'),
    'access arguments' => array('administer site configuration'),
42
    'file' => 'clientside_validation.admin.inc',
Jelle Sebreghts's avatar
Jelle Sebreghts committed
43
  );
44
  $items['clientside_validation/ajax'] = array(
Jelle Sebreghts's avatar
Jelle Sebreghts committed
45
46
47
48
    '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
49
50
51
52
  );
  return $items;
}

Jelle Sebreghts's avatar
Jelle Sebreghts committed
53
function _clientside_validation_ajax_call() {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  $param = $_POST['param'];
  $value = $_POST['value'];
  $result = array();
  foreach ($param['expressions'] as $key => $regex) {
    if (!(bool) preg_match($regex, (string) $value)) {
      $result =  array('result' => FALSE, 'message' => $param['messages'][$key]);
      break;
    }
  }
  if (empty($result)) {
    $result = array('result' => TRUE);
  }
  drupal_json_output($result);
}

Peter Droogmans's avatar
Peter Droogmans committed
69
/**
70
 * Implementation of hook_form_alter().
Peter Droogmans's avatar
Peter Droogmans committed
71
 */
72
function clientside_validation_form_alter(&$form, &$form_state, $form_id) {
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  $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
89
  if (!$page_match) {
90
91
    return;
  }
92
93
  $validate_specific_setting = variable_get('clientside_validation_validate_specific', '');
  $validate_specific = preg_split('/(\r\n?|\n)/', $validate_specific_setting, -1, PREG_SPLIT_NO_EMPTY);
94
95
96
97
98
99
  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)) {
100
101
    return;
  }
102
103
  if (strpos($form_id, 'webform_client_form') !== FALSE) {
    if (isset($form['actions']['previous'])) {
104
      $form['actions']['previous']['#attributes']['class'][] = 'cancel';
105
    }
106
    $form['#after_build'][] = 'clientside_validation_webform_after_build';
107
  }
108
  else {
109
110
111
    if (isset($form['actions']['delete'])) {
      $form['actions']['delete']['#attributes']['class'][] = 'cancel';
    }
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    switch ($form['#id']) {
      case 'node-form':
      case 'webform-component-edit-form':
        $form['#after_build'][] = 'clientside_validation_form_after_build';
        break;
      default:
        $form['#after_build'][] = 'clientside_validation_form_after_build';
        break;
    }
  }
}

/**
 * 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
130
  if (!empty($js_rules)) {
131
    $settings = array();
132
    _clientside_validation_add_general_settings($settings, $form['#id']);
Jelle's avatar
Jelle committed
133
    foreach ($js_rules as $key => $rule) {
Peter Droogmans's avatar
Peter Droogmans committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
      if (isset($rule['checkboxgroupminmax'])) {
        $settings['clientsideValidation']['forms'][$form['#id']]['checkboxrules'][$key] = array(
            'checkboxgroupminmax' => $rule['checkboxgroupminmax'],
            'messages' => array('checkboxgroupminmax' => $rule['messages']['checkboxgroupminmax'])
        );
        unset($rule['checkboxgroupminmax']);
        unset($rule['messages']['checkboxgroupminmax']);
        if (empty($rule['messages'])) {
          unset($rule['messages']);
        }
      }
      if (!empty($rule)) {
        $settings['clientsideValidation']['forms'][$form['#id']]['rules'][$key] = $rule;
      }
Peter Droogmans's avatar
Peter Droogmans committed
148
    }
Peter Droogmans's avatar
Peter Droogmans committed
149
150
151
152
153
154
155
    
    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');
    }
156
157
    //add our js file to the footer to support vertical_tabs
    drupal_add_js(drupal_get_path('module', 'clientside_validation') . '/clientside_validation.js', array('scope' => 'footer'));
Peter Droogmans's avatar
Peter Droogmans committed
158
159
160
161
162
163
    drupal_add_js($settings, 'setting');
  }
  return $form;
}

/**
164
 * Regular form.
Peter Droogmans's avatar
Peter Droogmans committed
165
166
167
 */
function clientside_validation_form_after_build(&$form, &$form_state) {
  static $js_rules = array();
168
169
170
171
  static $form_ids = array();
  if (isset($form_ids[$form['#id']])) {
    return $form;
  }
172
  
173
  drupal_alter("clientside_validation_form", $form, $form_state, $js_rules);
Peter Droogmans's avatar
Peter Droogmans committed
174
  if (!empty($js_rules)) {
175
    $settings = array();
176
    _clientside_validation_add_general_settings($settings, $form['#id']);
Jelle's avatar
Jelle committed
177
    foreach ($js_rules as $key => $rule) {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
      if (isset($rule['checkboxgroupminmax'])) {
        $settings['clientsideValidation']['forms'][$form['#id']]['checkboxrules'][$key] = array(
            'checkboxgroupminmax' => $rule['checkboxgroupminmax'],
            'messages' => array('checkboxgroupminmax' => $rule['messages']['checkboxgroupminmax'])
        );
        unset($rule['checkboxgroupminmax']);
        unset($rule['messages']['checkboxgroupminmax']);
        if (empty($rule['messages'])) {
          unset($rule['messages']);
        }
      }
      if (!empty($rule)) {
        $settings['clientsideValidation']['forms'][$form['#id']]['rules'][$key] = $rule;
      }
Peter Droogmans's avatar
Peter Droogmans committed
192
    }
Peter Droogmans's avatar
Peter Droogmans committed
193
194
195
196
197
198
    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');
    }
199
200
    //add our js file to the footer to support vertical_tabs
    drupal_add_js(drupal_get_path('module', 'clientside_validation') . '/clientside_validation.js', array('scope' => 'footer'));
Peter Droogmans's avatar
Peter Droogmans committed
201
    drupal_add_js($settings, 'setting');
202
    $form_ids[$form['#id']] = TRUE;
Peter Droogmans's avatar
Peter Droogmans committed
203
204
  }

205
  return $form;
Peter Droogmans's avatar
Peter Droogmans committed
206
207
}

208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
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);
  $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;
  }
  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;
    }
  }
  $form_settings_added[] = str_replace('-', '_', $form_id);
254
255
256
257
258
259
260
261
262
263
264
265
266
  $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),
    "validateOnKeyUp" => variable_get('clientside_validation_validate_onkeyup', 1),
    "validateOnSubmit" => variable_get('clientside_validation_validate_onsubmit', 1),
  );
}

267
268
269
/**
 * Set validation rule for required fields.
 */
Jelle's avatar
Jelle committed
270
function _clientside_validation_set_required($name, $title, $required, &$js_rules, $message = '') {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
271
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
Peter Droogmans's avatar
Peter Droogmans committed
272
273
  if ($required) {
    $js_rules[$name]['required'] = TRUE;
274
    $js_rules[$name]['messages']['required'] = (empty($message)) ? t('!name field is required.', array('!name' => $title)) : $message;
Peter Droogmans's avatar
Peter Droogmans committed
275
276
277
  }
}

278
279
280
/**
 * Set validation rule for number fields.
 */
281
function _clientside_validation_set_number($name, $title, &$js_rules) {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
282
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
Peter Droogmans's avatar
Peter Droogmans committed
283
284
285
286
  $js_rules[$name]['digits_negative'] = TRUE;
  $js_rules[$name]['messages']['digits_negative'] = t('!name field accepts only numbers.', array('!name' => $title));
}

287
288
289
/**
 * Set validation rule for decimal fields.
 */
290
function _clientside_validation_set_number_decimal($name, $title, $decimalpoint, &$js_rules) {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
291
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
Peter Droogmans's avatar
Peter Droogmans committed
292
293
294
295
296
297
298
299
300
301
  if ($decimalpoint == '.') {
    $js_rules[$name]['number'] = TRUE;
    $js_rules[$name]['messages']['number'] = t('!name field accepts only numbers (use a \'.\' as decimal point).', array('!name' => $title));
  }
  else {
    $js_rules[$name]['numberDE'] = TRUE;
    $js_rules[$name]['messages']['numberDE'] = t('!name field accepts only numbers (use a \',\' as decimal point).', array('!name' => $title));
  }
}

302
303
304
/**
 * Set validation rule for fields with a minimum and/or a maximum value.
 */
Jelle's avatar
Jelle committed
305
function _clientside_validation_set_minmax($name, $title, $min, $max, &$js_rules, $message = '') {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
306
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
307
  if (isset($min) && $min !== '' && isset($max) && $max !== '') {
Peter Droogmans's avatar
Peter Droogmans committed
308
    $js_rules[$name]['range'] = array($min, $max);
309
    $js_rules[$name]['messages']['range'] = (empty($message)) ? t('!name field has to be between !min and !max.', array('!name' => $title, '!min' => $min, '!max' => $max)) : $message;
Peter Droogmans's avatar
Peter Droogmans committed
310
  }
311
  elseif (isset($min) && $min !== '') {
Peter Droogmans's avatar
Peter Droogmans committed
312
    $js_rules[$name]['min'] = $min;
313
    $js_rules[$name]['messages']['min'] = (empty($message)) ? t('!name field has to be greater than !min.', array('!name' => $title, '!min' => $min)) : $message;
Peter Droogmans's avatar
Peter Droogmans committed
314
  }
315
  elseif (isset($max) && $max !== '') {
Peter Droogmans's avatar
Peter Droogmans committed
316
    $js_rules[$name]['max'] = $max;
317
    $js_rules[$name]['messages']['max'] = (empty($message)) ? t('!name field has to be smaller than !max.', array('!name' => $title, '!max' => $max)) : $message;
Peter Droogmans's avatar
Peter Droogmans committed
318
319
320
  }
}

321
322
323
/**
 * Set validation rule for fields with a minimum and/or maximum length.
 */
Jelle's avatar
Jelle committed
324
function _clientside_validation_set_minmaxlength($name, $title, $min, $max, &$js_rules, $message = '') {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
325
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
Jelle Sebreghts's avatar
Jelle Sebreghts committed
326
  if ($max == 1) {
327
328
    $max = '';
  }
329
  if (isset($min) && $min !== '' && isset($max) && $max !== '') {
Peter Droogmans's avatar
Peter Droogmans committed
330
    $js_rules[$name]['rangelength'] = array($min, $max);
331
    $js_rules[$name]['messages']['rangelength'] = (empty($message)) ? t('!name field has to have between !min and !max values.', array('!name' => $title, '!min' => $min, '!max' => $max)) : $message;
Peter Droogmans's avatar
Peter Droogmans committed
332
  }
333
  elseif (isset($min) && $min !== '') {
Peter Droogmans's avatar
Peter Droogmans committed
334
    $js_rules[$name]['minlength'] = $min;
335
    $js_rules[$name]['messages']['minlength'] = (empty($message)) ? t('!name field has to have minimal !min values.', array('!name' => $title, '!min' => $min)) : $message;
Peter Droogmans's avatar
Peter Droogmans committed
336
  }
337
  elseif (isset($max) && $max !== '') {
Peter Droogmans's avatar
Peter Droogmans committed
338
    $js_rules[$name]['maxlength'] = $max;
339
    $js_rules[$name]['messages']['maxlength'] = (empty($message)) ? t('!name field has to have maximum !max values.', array('!name' => $title, '!max' => $max)) : $message;
Peter Droogmans's avatar
Peter Droogmans committed
340
341
342
  }
}

343
344
345
/**
 * Set validation rule for required fields that must equal a value from an other field.
 */
Jelle's avatar
Jelle committed
346
function _clientside_validation_set_equal($name, $title, $value, &$js_rules) {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
347
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
348
349
350
351
352
353
354
  $js_rules[$name]['equalTo'] = ':input[name=\'submitted[' . $value['form_key'] . ']\']';
  $js_rules[$name]['messages']['equalTo'] = t('!name field has to be equal to !firstone.', array('!name' => $title, '!firstone' => $value['name']));
}

/**
 * Set validation rule for fields that can not be equal to a value from an other field.
 */
Jelle's avatar
Jelle committed
355
function _clientside_validation_set_not_equal($name, $title, $value, &$js_rules, $message = '') {
356
  $js_rules[$name]['notEqualTo'] = ':input[name=\'submitted[' . $value['form_key'] . ']\']';
Jelle Sebreghts's avatar
Jelle Sebreghts committed
357
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
358
359
360
361
362
363
364
  $message = empty($message) ? t('!name field has to different from !firstone.', array('!name' => $title, '!firstone' => $value['name'])) : $message;
  $js_rules[$name]['messages']['notEqualTo'] = $message;
}

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

372
373
374
375
376
377
378
379
380
381
/**
 * Set validation rule for fields that consist of one or more of specific values (e.g. string with only a, b and c in it).
 */
function _clientside_validation_set_specific_values($name, $title, $values, &$js_rules, $message = '') {
  $js_rules[$name]['specificVals'] = $values;
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
  $message = empty($message) ? t('!name field must consist of following elements only: !elements.', array('!name' => $title, '!elements' => implode(', ', $values))) : $message;
  $js_rules[$name]['messages']['specificVals'] = $message;
}

382
383
384
/**
 * Set validation rule for ean number fields.
 */
Jelle's avatar
Jelle committed
385
function _clientside_validation_set_ean($name, $title, &$js_rules, $message = '') {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
386
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
387
388
  $message = empty($message) ? t('!name field is not a valid EAN number.', array('!name' => $title)) : $message;
  $js_rules[$name]['validEAN'] = TRUE;
Peter Droogmans's avatar
Peter Droogmans committed
389
  $js_rules[$name]['messages']['validEAN'] = $message;
Peter Droogmans's avatar
Peter Droogmans committed
390
391
}

392
393
394
/**
 * Set validation rule for fields with regex validation from webform_validation
 */
395
function _clientside_validation_set_regex($name, $title, &$js_rules, $expression, $message = '', $modifiers = "") {
396
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
397
  $message = empty($message) ? t('!name field is not match the given format.', array('!name' => $title)) : $message;
398
  $js_rules[$name]['regexMatch'] = array($expression);
Jelle Sebreghts's avatar
Jelle Sebreghts committed
399
  if (!empty($modifiers)) {
400
401
    $js_rules[$name]['regexMatch'][] = $modifiers;
  }
402
403
404
  $js_rules[$name]['messages']['regexMatch'] = $message;
}

Jelle Sebreghts's avatar
Jelle Sebreghts committed
405
406
407
408
409
410
411
412
413
414
/**
 * Set validation rule for fields with regex validation from webform_validation
 */
function _clientside_validation_set_regex_pcre($name, $title, &$js_rules, $expression, $message = '') {
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
  $message = empty($message) ? t('!name field is not a valid EAN number.', array('!name' => $title)) : $message;
  $js_rules[$name]['regexMatchPCRE']['expressions'][] = $expression;
    $js_rules[$name]['regexMatchPCRE']['messages'][] = $message;
}

415
416
417
/**
 * Set validation rule for file fields that must have a certain extension.
 */
Jelle's avatar
Jelle committed
418
function _clientside_validation_set_extensions($name, $extensions, &$js_rules) {
419
420
421
422
423
424
425
426
  $extension_list = preg_replace('#,(?![^,]+,)#', ' or', implode(', ', $extensions));
  $js_rules[$name]['accept'] = implode('|', $extensions);
  $js_rules[$name]['messages']['accept'] = t("Only files with a %exts extension are allowed.", array('%exts' => $extension_list));
}

/**
 * Set validation rule for checkboxes.
 */
427
function _clientside_validation_set_checkboxgroup_minmax($name, $title, $id, &$js_rules, $message = '', $min = 1, $max = 99) {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
428
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
429
  $message = empty($message) ? t('!name field is required.', array('!name' => $title)) : $message;
430
  $js_rules[$name]['checkboxgroupminmax'] = array($min, $max, $id);
431
  $js_rules[$name]['messages']['checkboxgroupminmax'] = $message;
432
433
434
435
436
}

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

443
444
445
/**
 * Set validation rule for url fields.
 */
Jelle Sebreghts's avatar
Jelle Sebreghts committed
446
function _clientside_validation_set_url($name, $title, &$js_rules) {
447
448
449
450
451
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
  $js_rules[$name]['url'] = TRUE;
  $js_rules[$name]['messages']['url'] = t('The value in !name is not a valid url.', array('!name' => $title));
}

452
453
function clientside_validation_drupal_json_encode($var) {
  return str_replace(array('<', '>', '&'), array('\u003c', '\u003e', '\u0026'), json_encode($var));
454
}