clientside_validation.module 27.7 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
5
6
7
8
9
10
11
12
/**
 * @file
 * Add client side validation to a webform.
 */

/**
 * Implementation of hook_form_alter().
 */
function clientside_validation_form_alter(&$form, &$form_state, $form_id) {
  if (strpos($form_id, 'webform_client_form') !== FALSE) {
Peter Droogmans's avatar
Peter Droogmans committed
13
14
15
16
17
18
19
20
21
22
23
24
25
    if (isset($form['actions']['previous'])) {
      if (isset($form['actions']['previous']['#attributes'])) {
        if (isset($form['actions']['previous']['#attributes']['class'])) {
          $form['actions']['previous']['#attributes']['class'] .= ' cancel';
        }
        else {
          $form['actions']['previous']['#attributes']['class'] = 'cancel';
        }
      }
      else {
        $form['actions']['previous']['#attributes']['class'] = 'cancel';
      }
    }
Peter Droogmans's avatar
Peter Droogmans committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    $form['#after_build'][] = 'clientside_validation_webform_after_build';
  } 
  else {
    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;
    }
  }
}

Peter Droogmans's avatar
Peter Droogmans committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
 * Implements hook_menu().
 */
function clientside_validation_menu() {
  $items['admin/structure/clientside_validation'] = array(
    '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'),
  );
  return $items;
}

function clientside_validation_admin_settings() {
  $form['clientside_validation'] = array(
    '#type' => 'fieldset',
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#title' => t('Field name formatting'),
    '#description' => t('You can define the prefix and suffix of the field names in the error messages here.'),
  );
  $form['clientside_validation']['clientside_validation_prefix'] = array(
    '#title' => t('Field name prefix'),
    '#description' => t('The prefix of the field name in the error messages.'),
    '#type' => 'textfield',
    '#default_value' => variable_get('clientside_validation_prefix', ''),
  );
  $form['clientside_validation']['clientside_validation_suffix'] = array(
    '#title' => t('Field name suffix'),
    '#description' => t('The suffix of the field name in the error messages.'),
    '#type' => 'textfield',
    '#default_value' => variable_get('clientside_validation_suffix', ''),
  );
  $form['clientside_validation']['example_image'] = array(
    '#type' => 'item',
    '#title' => t('Example'),
    '#value' => '<img id="clientside_validation_example_image" src="' . base_path() . drupal_get_path('module', 'clientside_validation') . '/errormsg.png" alt="' . t('Error message example') . '" />',
    '#description' => t('Filling in double quotes in both the above fields will give this result.'),
  );

  $form['clientside_validation_min'] = array(
    '#type' => 'fieldset',
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#title' => t('Use minified version of jquery.validate.js'),
    '#description' => t('You can use the minified version of the library.'),
  );
  $form['clientside_validation_min']['clientside_validation_use_minified'] = array(
    '#type' => 'radios',
    '#options' => array(
      '0' => t('No'), 
      '1' => t('Yes')
    ),
    '#default_value' => variable_get('clientside_validation_use_minified', 0), 
    '#title' => t('Use minified version?'),
  );
  
  return system_settings_form($form);
}

Peter Droogmans's avatar
Peter Droogmans committed
102
/**
103
 * Webform.
Peter Droogmans's avatar
Peter Droogmans committed
104
105
106
 */
function clientside_validation_webform_after_build(&$form, &$form_state) {
  $js_rules = array();
107
  clientside_validation_webform_after_build_recurse($form['#id'], $form, $form_state, $js_rules);
Peter Droogmans's avatar
Peter Droogmans committed
108
  if ($webform_validation_rules = clientside_validation_webform_validation($form_state['values']['details']['nid'])) {
109
110
111
112
    foreach ($webform_validation_rules as $webform_validation_rule) {
      switch ($webform_validation_rule['validator']) {
        case 'min_length':
          foreach ($webform_validation_rule['components'] as $component) {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
113
          $message = t('!name field has a minimum length of !minl characters.', array('!name' => variable_get('clientside_validation_prefix', '') . $component['name'] . variable_get('clientside_validation_suffix', ''), '!minl' => $webform_validation_rule['data']));
114
115
116
117
118
           _clientside_validation_set_minmaxlength('submitted[' . $component['form_key'] . ']', $component['name'], $webform_validation_rule['data'], '', $js_rules, $message);
          }
          break;
        case 'max_length':
          foreach ($webform_validation_rule['components'] as $component) {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
119
            $message = t('!name field has a maximum length of !maxl characters.', array('!name' => variable_get('clientside_validation_prefix', '') . $component['name'] . variable_get('clientside_validation_suffix', ''), '!maxl' => $webform_validation_rule['data']));
Jelle's avatar
Jelle committed
120
            _clientside_validation_set_minmaxlength('submitted[' . $component['form_key'] . ']', $component['name'], '', $webform_validation_rule['data'], $js_rules, $message);
Peter Droogmans's avatar
Peter Droogmans committed
121

122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
          }
          break;
        case 'numeric':
          foreach ($webform_validation_rule['components'] as $component) {
            $range = explode('-', $webform_validation_rule['data']);
            if (!empty($range[0]) && $range[0] != '0') {
              if (!isset($range[1])) {
                $range[1] = $range[0];
                $range[0] = 0;
              }
              _clientside_validation_set_minmax('submitted[' . $component['form_key'] . ']', $component['name'], $range[0], $range[1], $js_rules);
            }
          }
          break;
        case 'equal':
          $others = $webform_validation_rule['components'];
          $firstone = array_shift($others);
          foreach ($others as $component) {
            _clientside_validation_set_equal('submitted[' . $component['form_key'] . ']', $component['name'], $firstone, $js_rules);
          }
          break;
        case 'unique':
          $all = $webform_validation_rule['components'];
          while (count($all) > 1) {
            $firstone = array_shift($all);
            foreach ($all as $component) {
              $message = (isset($webform_validation_rule['error_message']) && !empty($webform_validation_rule['error_message'])) ? $webform_validation_rule['error_message'] : '';
              _clientside_validation_set_not_equal('submitted[' . $component['form_key'] . ']', $component['name'], $firstone, $js_rules, $message);
            }
          }
          break;
        case 'specific_value':
          foreach ($webform_validation_rule['components'] as $component) {
            $value = explode(',', $webform_validation_rule['data']);
            $message = (isset($webform_validation_rule['error_message']) && !empty($webform_validation_rule['error_message'])) ? $webform_validation_rule['error_message'] : '';
            _clientside_validation_set_specific_value('submitted[' . $component['form_key'] . ']', $component['name'], $value, $js_rules, $message);
          }
          break;
        case 'select_min':
          foreach ($webform_validation_rule['components'] as $component) {
Jelle's avatar
Jelle committed
162
            _clientside_validation_set_minmaxlength('submitted[' . $component['form_key'] . ']', $component['name'], $webform_validation_rule['data'], '', $js_rules);
163
164
165
166
          }
          break;
        case 'select_max':
          foreach ($webform_validation_rule['components'] as $component) {
Jelle's avatar
Jelle committed
167
            _clientside_validation_set_minmaxlength('submitted[' . $component['form_key'] . ']', $component['name'], '', $webform_validation_rule['data'], $js_rules);
168
169
170
171
          }
          break;
        case 'select_exact':
          foreach ($webform_validation_rule['components'] as $component) {
Jelle's avatar
Jelle committed
172
            _clientside_validation_set_minmaxlength('submitted[' . $component['form_key'] . ']', $component['name'], $webform_validation_rule['data'], $webform_validation_rule['data'], $js_rules);
173
174
175
176
          }
          break;
        case 'validEAN':
          foreach ($webform_validation_rule['components'] as $component) {
Peter Droogmans's avatar
Peter Droogmans committed
177
            $message = (isset($webform_validation_rule['error_message']) && !empty($webform_validation_rule['error_message'])) ? $webform_validation_rule['error_message'] : 'Not a valid EAN number.';
178
179
180
            _clientside_validation_set_ean('submitted[' . $component['form_key'] . ']', $component['name'], $js_rules, $message);
          }
          break;
181
182
183
184
185
186
187
        case 'regex':
          foreach ($webform_validation_rule['components'] as $component) {
            $message = $webform_validation_rule['error_message'];
            $expression = $webform_validation_rule['data'];
            _clientside_validation_set_regex('submitted[' . $component['form_key'] . ']', $component['name'], $js_rules, $expression, $message);
          }
          break;
188
189
190
      }
    }
  }
Peter Droogmans's avatar
Peter Droogmans committed
191
  if (!empty($js_rules)) {
Jelle's avatar
Jelle committed
192
    $settings['clientsideValidation']['general'] = array(
Peter Droogmans's avatar
Peter Droogmans committed
193
194
195
196
197
198
199
      "errorClass" => "error",
      "wrapper" => "li",
    );
    $settings['clientsideValidation']['forms'][$form['#id']]['settings'] = array(
      "errorContainer" => "#formerrors-" . $form['#id'],
      "errorLabelContainer" => "#formerrors-" . $form['#id'] . " ul",
    );
Jelle's avatar
Jelle committed
200
    foreach ($js_rules as $key => $rule) {
Peter Droogmans's avatar
Peter Droogmans committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
      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
215
    }
Peter Droogmans's avatar
Peter Droogmans committed
216
217
218
219
220
221
222
    
    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');
    }
Peter Droogmans's avatar
Peter Droogmans committed
223
    drupal_add_js(drupal_get_path('module', 'clientside_validation') . '/clientside_validation.js');
Peter Droogmans's avatar
Peter Droogmans committed
224
225
226
227
228
229
230
231
    drupal_add_js($settings, 'setting');
  }
  return $form;
}

function clientside_validation_webform_after_build_recurse($form_id, &$form, &$form_state, &$js_rules) {
  if ($children = array_values(element_children($form))) {
    foreach ($children as $index => $item) {
Peter Droogmans's avatar
Peter Droogmans committed
232
      $element = $form[$item];
Peter Droogmans's avatar
Peter Droogmans committed
233
234
235
      if (isset($element['#title'])) {
        if ($element['#required']) {
          if (isset($element['#webform_component']) && $element['#webform_component']['type'] == 'time' && isset($element['hour']['#name'])) {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
236
            $message = t('Hour in !name field is required.', array('!name' => variable_get('clientside_validation_prefix', '') . $element['#title'] . variable_get('clientside_validation_suffix', '')));
237
            _clientside_validation_set_required($element['hour']['#name'], $element['#title'], TRUE, $js_rules, $message);
Jelle Sebreghts's avatar
Jelle Sebreghts committed
238
            $message = t('Minute in !name field is required.', array('!name' => variable_get('clientside_validation_prefix', '') . $element['#title'] . variable_get('clientside_validation_suffix', '')));
239
            _clientside_validation_set_required($element['minute']['#name'], $element['#title'], TRUE, $js_rules, $message);
Peter Droogmans's avatar
Peter Droogmans committed
240
          }
Jelle's avatar
Jelle committed
241
          elseif (isset($element['#webform_component']) && $element['#webform_component']['type'] == 'date') {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
242
            $message = t('Month in !name field is required.', array('!name' => variable_get('clientside_validation_prefix', '') . $element['#title'] . variable_get('clientside_validation_suffix', '')));
243
            _clientside_validation_set_required($element['#name'] . '[month]', $element['#title'], TRUE, $js_rules, $message);
Jelle Sebreghts's avatar
Jelle Sebreghts committed
244
            $message = t('Day in !name field is required.', array('!name' => variable_get('clientside_validation_prefix', '') . $element['#title'] . variable_get('clientside_validation_suffix', '')));
245
            _clientside_validation_set_required($element['#name'] . '[day]', $element['#title'], TRUE, $js_rules, $message);
Jelle Sebreghts's avatar
Jelle Sebreghts committed
246
            $message = t('Year in !name field is required.', array('!name' => variable_get('clientside_validation_prefix', '') . $element['#title'] . variable_get('clientside_validation_suffix', '')));
247
            _clientside_validation_set_required($element['#name'] . '[year]', $element['#title'], TRUE, $js_rules, $message);
Peter Droogmans's avatar
Peter Droogmans committed
248
            if (is_numeric($element['#year_start']) && is_numeric($element['#year_end'])) {
249
250
              $message = t('The entered date needs to be between the years @start and @end.', array('@start' => $element['#year_start'], '@end' => $element['#year_end']));
              _clientside_validation_set_minmax($element['#name'] . '[year]', $element['#title'], $element['#year_start'], $element['#year_end'], $js_rules, $message);
Peter Droogmans's avatar
Peter Droogmans committed
251
            }            
Peter Droogmans's avatar
Peter Droogmans committed
252
          }
Jelle's avatar
Jelle committed
253
          elseif ($element['#type'] == 'checkboxes') {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
254
255
            $id = '#' . $element['#id'];
            _clientside_validation_set_checkboxgroup_minmax($element['#name'], $element['#title'], $id, $js_rules);
Peter Droogmans's avatar
Peter Droogmans committed
256
          }
Jelle's avatar
Jelle committed
257
          elseif ($element['#type'] == 'select' && $element['#multiple']) {
258
            _clientside_validation_set_required($element['#name'] . '[]', $element['#title'], TRUE, $js_rules);
Peter Droogmans's avatar
Peter Droogmans committed
259
          }
Jelle's avatar
Jelle committed
260
          elseif (isset($element['#type'])) {
261
            _clientside_validation_set_required($element['#name'], $element['#title'], TRUE, $js_rules);
Peter Droogmans's avatar
Peter Droogmans committed
262
263
264
          }
        }
        if (isset($element['#webform_component']) && $element['#webform_component']['type'] == 'file' && $element['#webform_component']['mandatory'] == "1") {
Peter Droogmans's avatar
Peter Droogmans committed
265
          _clientside_validation_set_required($element['#name'], $element['#title'], TRUE, $js_rules);
Peter Droogmans's avatar
Peter Droogmans committed
266
267
          if (isset($element['#webform_component']['extra']['filtering']['types'])) {
            $extensions = $element['#webform_component']['extra']['filtering']['types'];
268
            _clientside_validation_set_extensions($element['#name'], $extensions, $js_rules);
Peter Droogmans's avatar
Peter Droogmans committed
269
270
          }
        }
Peter Droogmans's avatar
Peter Droogmans committed
271
        if (isset($element['#maxlength']) && $element['#maxlength']) {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
272
          $message = t('!name field has a max length of !maxl characters.', array('!name' => variable_get('clientside_validation_prefix', '') . $element['#title'] . variable_get('clientside_validation_suffix', ''), '!maxl' => $element['#maxlength']));
273
          _clientside_validation_set_minmaxlength($element['#name'], $element['#title'], '', $element['#maxlength'], $js_rules, $message);
Peter Droogmans's avatar
Peter Droogmans committed
274
        }
275
        
Peter Droogmans's avatar
Peter Droogmans committed
276
        if (isset($element['#webform_component']) && $element['#webform_component']['type'] == 'email') {
277
          _clientside_validation_set_email($element['#name'], $element['#title'], $js_rules);
Peter Droogmans's avatar
Peter Droogmans committed
278
279
280
281
282
283
284
        }
      }
      clientside_validation_webform_after_build_recurse($form_id, $element, $form_state, $js_rules);
    }
  }
}

285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
 * Implements hook_webform_validation().
 */
function clientside_validation_webform_validation($nid) {
  static $webform_validation_rules;
  if (!isset($webform_validation_rules[$nid])) {
    if (module_exists('webform_validation')) {
      $webform_validation_rules[$nid] = webform_validation_get_node_rules($nid);
    }
    else {
      $webform_validation_rules[$nid] = NULL;
    }
  }
  return $webform_validation_rules[$nid];
}

Peter Droogmans's avatar
Peter Droogmans committed
301
/**
Peter Droogmans's avatar
Peter Droogmans committed
302
 * Regular form
Peter Droogmans's avatar
Peter Droogmans committed
303
304
305
 */
function clientside_validation_form_after_build(&$form, &$form_state) {
  static $js_rules = array();
306
  clientside_validation_form_after_build_recurse($form['#id'], $form, $form_state, $js_rules);
Peter Droogmans's avatar
Peter Droogmans committed
307
  if (!empty($js_rules)) {
Jelle's avatar
Jelle committed
308
    $settings['clientsideValidation']['general'] = array(
Peter Droogmans's avatar
Peter Droogmans committed
309
310
311
312
313
314
315
      "errorClass" => "error",
      "wrapper" => "li",
    );
    $settings['clientsideValidation']['forms'][$form['#id']]['settings'] = array(
      "errorContainer" => "#formerrors-" . $form['#id'],
      "errorLabelContainer" => "#formerrors-" . $form['#id'] . " ul",
    );
Jelle's avatar
Jelle committed
316
    foreach ($js_rules as $key => $rule) {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
317
318
319
320
321
322
323
324
325
326
327
328
329
330
      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
331
    }
Peter Droogmans's avatar
Peter Droogmans committed
332
333
334
335
336
337
    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');
    }
Peter Droogmans's avatar
Peter Droogmans committed
338
    drupal_add_js(drupal_get_path('module', 'clientside_validation') . '/clientside_validation.js');
Peter Droogmans's avatar
Peter Droogmans committed
339
340
341
342
343
344
345
346
347
348
    drupal_add_js($settings, 'setting');
  }
  
  return $form;
}

function clientside_validation_form_after_build_recurse($form_id, &$form, &$form_state, &$js_rules) {
  if ($children = array_values(element_children($form))) {
    foreach ($children as $index => $item) {
      $element = &$form[$item];
349
350
351
352
      $types = array(
        'textfield', 'textarea', 'select', 'radio', 'checkbox', 'password', 'file', 'radios', 'checkboxes',
      );
      if (isset($element['#type']) && in_array($element['#type'], $types)) {
Jelle's avatar
Jelle committed
353
        clientside_validation_regular($form_id, $element, $js_rules);
Peter Droogmans's avatar
Peter Droogmans committed
354
355
356
357
358
359
      }
      clientside_validation_form_after_build_recurse($form_id, $element, $form_state, $js_rules);
    }
  }
}

Jelle's avatar
Jelle committed
360
function clientside_validation_regular($form_id, $element, &$js_rules) {
Peter Droogmans's avatar
Peter Droogmans committed
361
362
363
  static $multiples = array();
  if (isset($element['#name']) && !isset($js_rules[$element['#name']])) {
    $el_name = $element['#name'];
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
    $el_title = $el_name;
    if (isset($element['#title'])) {
      $el_title = $element['#title'];
    }
    $is_multiple = FALSE;
    if (isset($element['#multiple'])) {
      $is_multiple = $element['#multiple'];
    }
    switch ($element['#type']) {
      case 'textfield':
      case 'password':
      case 'textarea':
      case 'file':
        if ($is_multiple) {
          // Only first field is required
          if (!isset($multiples[$form_id][$el_name])) {
Jelle's avatar
Jelle committed
380
            _clientside_validation_set_required($el_name, $el_title, $element['#required'], $js_rules);
381
382
            $multiples[$form_id][$el_name] = 1;
          }
Peter Droogmans's avatar
Peter Droogmans committed
383
        }
384
        else {
Jelle's avatar
Jelle committed
385
          _clientside_validation_set_required($el_name, $el_title, $element['#required'], $js_rules);
386
        }
387
388
        if (isset($element['#maxlength']) && $element['#maxlength'] > 0) {
          _clientside_validation_set_minmaxlength($el_name, $el_title, '', $element['#maxlength'], $js_rules);
Peter Droogmans's avatar
Peter Droogmans committed
389
390
        }
        break;
391
392
393
      case 'select':
        if ($is_multiple) {
          $el_name .= '[]';
Jelle's avatar
Jelle committed
394
          if (!isset($element['#minlength'])) {
395
396
            $element['#minlength'] = 0;
          }
Peter Droogmans's avatar
Peter Droogmans committed
397
          $max = (($is_multiple === TRUE || $is_multiple == 1) ? '' : $is_multiple);
Jelle Sebreghts's avatar
Jelle Sebreghts committed
398
          _clientside_validation_set_minmaxlength($el_name, $el_title, $element['#minlength'], $max, $js_rules);
Peter Droogmans's avatar
Peter Droogmans committed
399
        }
Jelle's avatar
Jelle committed
400
        _clientside_validation_set_required($el_name, $el_title, $element['#required'], $js_rules);
Peter Droogmans's avatar
Peter Droogmans committed
401
        break;
402
403
      case 'radio':
      case 'radios':
Jelle's avatar
Jelle committed
404
        _clientside_validation_set_required($el_name, $el_title, $element['#required'], $js_rules);
405
406
407
408
        break;
      case 'checkbox':
      case 'checkboxes':
        $is_multiple = (isset($element['#options']) && count($element['#options']>1));
Peter Droogmans's avatar
Peter Droogmans committed
409
        if ($is_multiple && $element['#required']) {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
410
411
          $id = '#' . $element['#id'];
          _clientside_validation_set_checkboxgroup_minmax($element['#name'], $el_title, $id, $js_rules);
412
        }
Jelle Sebreghts's avatar
Jelle Sebreghts committed
413
        elseif ($element['#required']) {
Jelle's avatar
Jelle committed
414
          _clientside_validation_set_required($el_name, $el_title, $element['#required'], $js_rules);
415
        }
Peter Droogmans's avatar
Peter Droogmans committed
416
417
418
419
420
        break;
    }
  }
}

421
422
423
/**
 * Set validation rule for required fields.
 */
Jelle's avatar
Jelle committed
424
function _clientside_validation_set_required($name, $title, $required, &$js_rules, $message = '') {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
425
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
Peter Droogmans's avatar
Peter Droogmans committed
426
427
  if ($required) {
    $js_rules[$name]['required'] = TRUE;
428
    $js_rules[$name]['messages']['required'] = (empty($message)) ? t('!name field is required.', array('!name' => $title)) : $message;
Peter Droogmans's avatar
Peter Droogmans committed
429
430
431
  }
}

432
433
434
/**
 * Set validation rule for number fields.
 */
Jelle's avatar
Jelle committed
435
function _clientside_validation_set_number($name, $title, $decimalpoint, &$js_rules) {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
436
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
Peter Droogmans's avatar
Peter Droogmans committed
437
438
439
440
  $js_rules[$name]['digits_negative'] = TRUE;
  $js_rules[$name]['messages']['digits_negative'] = t('!name field accepts only numbers.', array('!name' => $title));
}

441
442
443
/**
 * Set validation rule for decimal fields.
 */
Jelle's avatar
Jelle committed
444
function _clientside_validation_set_number_decimal($name, $title, $decimalpoint, &$js_rules) {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
445
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
Peter Droogmans's avatar
Peter Droogmans committed
446
447
448
449
450
451
452
453
454
455
  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));
  }
}

456
457
458
/**
 * Set validation rule for fields with a minimum and/or a maximum value.
 */
Jelle's avatar
Jelle committed
459
function _clientside_validation_set_minmax($name, $title, $min, $max, &$js_rules, $message = '') {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
460
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
Peter Droogmans's avatar
Peter Droogmans committed
461
462
  if (isset($min) && $min != '' && isset($max) && $max != '') {
    $js_rules[$name]['range'] = array($min, $max);
463
    $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
464
  }
Jelle's avatar
Jelle committed
465
  elseif (isset($min) && $min != '') {
Peter Droogmans's avatar
Peter Droogmans committed
466
    $js_rules[$name]['min'] = $min;
467
    $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
468
  }
Jelle's avatar
Jelle committed
469
  elseif (isset($max) && $max != '') {
Peter Droogmans's avatar
Peter Droogmans committed
470
    $js_rules[$name]['max'] = $max;
471
    $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
472
473
474
  }
}

475
476
477
/**
 * Set validation rule for fields with a minimum and/or maximum length.
 */
Jelle's avatar
Jelle committed
478
function _clientside_validation_set_minmaxlength($name, $title, $min, $max, &$js_rules, $message = '') {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
479
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
480
481
482
  if($max == 1){
    $max = '';
  }
Peter Droogmans's avatar
Peter Droogmans committed
483
484
  if (isset($min) && $min != '' && isset($max) && $max != '') {
    $js_rules[$name]['rangelength'] = array($min, $max);
485
    $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
486
  }
Jelle's avatar
Jelle committed
487
  elseif (isset($min) && $min != '') {
Peter Droogmans's avatar
Peter Droogmans committed
488
    $js_rules[$name]['minlength'] = $min;
489
    $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
490
  }
Jelle's avatar
Jelle committed
491
  elseif (isset($max) && $max != '') {
Peter Droogmans's avatar
Peter Droogmans committed
492
    $js_rules[$name]['maxlength'] = $max;
493
    $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
494
495
496
  }
}

497
498
499
/**
 * Set validation rule for required fields that must equal a value from an other field.
 */
Jelle's avatar
Jelle committed
500
function _clientside_validation_set_equal($name, $title, $value, &$js_rules) {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
501
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
502
503
504
505
506
507
508
  $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
509
function _clientside_validation_set_not_equal($name, $title, $value, &$js_rules, $message = '') {
510
  $js_rules[$name]['notEqualTo'] = ':input[name=\'submitted[' . $value['form_key'] . ']\']';
Jelle Sebreghts's avatar
Jelle Sebreghts committed
511
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
512
513
514
515
516
517
518
  $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
519
function _clientside_validation_set_specific_value($name, $title, $value, &$js_rules, $message = '') {
520
  $js_rules[$name]['oneOf'] = $value;
Jelle Sebreghts's avatar
Jelle Sebreghts committed
521
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
522
523
524
525
526
527
528
  $message = empty($message) ? t('!name field has to different from !firstone.', array('!name' => $title, '!firstone' => $value['name'])) : $message;
  $js_rules[$name]['messages']['oneOf'] = $message;
}

/**
 * Set validation rule for ean number fields.
 */
Jelle's avatar
Jelle committed
529
function _clientside_validation_set_ean($name, $title, &$js_rules, $message = '') {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
530
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
531
532
  $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
533
  $js_rules[$name]['messages']['validEAN'] = $message;
Peter Droogmans's avatar
Peter Droogmans committed
534
535
}

536
537
538
539
540
541
542
543
544
545
/**
 * Set validation rule for fields with regex validation from webform_validation
 */
function _clientside_validation_set_regex($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]['regexMatch'] = $expression;
  $js_rules[$name]['messages']['regexMatch'] = $message;
}

546
547
548
/**
 * Set validation rule for file fields that must have a certain extension.
 */
Jelle's avatar
Jelle committed
549
function _clientside_validation_set_extensions($name, $extensions, &$js_rules) {
550
551
552
553
554
555
556
557
  $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.
 */
Jelle Sebreghts's avatar
Jelle Sebreghts committed
558
559
function _clientside_validation_set_checkboxgroup_minmax($name, $title, $id, &$js_rules, $min = 1, $max = 99) {
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
560
  $js_rules[$name]['checkboxgroupminmax'] = array($min, $max, $id);
Jelle Sebreghts's avatar
Jelle Sebreghts committed
561
  $js_rules[$name]['messages']['checkboxgroupminmax'] = t('!name field is required.', array('!name' => $title));
562
563
564
565
566
}

/**
 * Set validation rule for email fields.
 */
Jelle's avatar
Jelle committed
567
function _clientside_validation_set_email($name, $title, &$js_rules) {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
568
  $title = variable_get('clientside_validation_prefix', '') . $title . variable_get('clientside_validation_suffix', '');
569
570
571
572
573
574
575
  $js_rules[$name]['email'] = TRUE;
  $js_rules[$name]['messages']['email'] = t('The value in !name is not a valid email address.', array('!name' => $title));
}

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