clientside_validation.module 44.9 KB
Newer Older
1001
  $js_rules[$name]['email'] = TRUE;
1002
  $variables = array(
1003
1004
    'message' => empty($message) ? 'The value in !title is not a valid email address.' : $message,
    'placeholders' => empty($message) ? array('!title' => $title) : array(),
1005
1006
1007
1008
    'error_type' => 'email',
    'element_name' => $name
  );
  $js_rules[$name]['messages']['email'] = theme('clientside_error', $variables);
1009
1010
}

1011
1012
1013
/**
 * Set validation rule for url fields.
 */
1014
function _clientside_validation_set_url($name, $title, &$js_rules, $message = '') {
Peter Droogmans's avatar
Peter Droogmans committed
1015
  $title = _clientside_validation_set_title($title);
1016
  $js_rules[$name]['url'] = TRUE;
1017
  $variables = array(
1018
1019
    'message' => empty($message) ? 'The value in !title is not a valid url.' : $message,
    'placeholders' => empty($message) ? array('!title' => $title) : array(),
1020
1021
1022
1023
    'error_type' => 'url',
    'element_name' => $name
  );
  $js_rules[$name]['messages']['url'] = theme('clientside_error', $variables);
1024
1025
}

1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
/**
 * Set validation rule for "require at least one of several".
 */
function _clientside_validation_set_require_oneof($names, $titles, $message = "", &$js_rules) {
  $original_message = $message;
  foreach ($names as $key => $name) {
    $temp_titles = $titles;
    unset($temp_titles[$key]);
    if (empty ($original_message)) {
      $string_titles = '<ul><li>' . implode('</li><li>', $temp_titles) . '</li></ul>';
      $message = t('!name field is required unless you specify at least one of these items: !items', array('!name' => $titles[$key], '!items' => $string_titles));
    }
    $js_rules[$name]['requireOneOf'] = array_diff($names, array($name));
    $variables = array(
      'message' => $message,
      'error_type' => 'requireOneOf',
      'element_name' => $name,
    );
    $js_rules[$name]['messages']['requireOneOf'] = theme('clientside_error', $variables);
  }
}

1048
function _clientside_validation_set_phone($name, $title, $country_code, &$js_rules, $message = "") {
Peter Droogmans's avatar
Peter Droogmans committed
1049
  $title = _clientside_validation_set_title($title);
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
  $js_rules[$name]['phone'] = $country_code;
  $variables = array(
    'message' => empty($message) ? 'The value in !title is not a valid phone number.' : $message,
    'placeholders' => empty($message) ? array('!title' => $title) : array(),
    'error_type' => 'phone',
    'element_name' => $name
  );
  $js_rules[$name]['messages']['phone'] = theme('clientside_error', $variables);
}

1060
1061
function clientside_validation_drupal_json_encode($var) {
  return str_replace(array('<', '>', '&'), array('\u003c', '\u003e', '\u0026'), json_encode($var));
1062
}
1063
1064
1065
1066
1067

/**
 * Implements hook_theme().
 */
function clientside_validation_theme($existing, $type, $theme, $path) {
Jelle Sebreghts's avatar
Jelle Sebreghts committed
1068
  return array(
1069
1070
1071
1072
    'clientside_error' => array(
      'variables' => array(
        'message' => '',
        'placeholders' => array(),
1073
1074
        'error_type' => '',
        'element_name' => '',
1075
1076
1077
1078
1079
1080
1081
      )
    )
  );
}

/**
 * Theme callback function.
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
 * @param array $variables
 * An array with the following keys:
 *  'message':
 *  A string containing the error message
 *  'placeholders':
 *  An associative array of replacements to make after translation.
 *  Incidences of any key in this array are replaced with the corresponding value.
 *  Based on the first character of the key, the value is escaped and/or themed:
 *    !variable: inserted as is
 *    @variable: escape plain text to HTML (check_plain)
 *    %variable: escape text and theme as a placeholder for user-submitted content (check_plain + theme_placeholder)
 *  'error_type':
 *  The error type of this error message (e.g. 'required', 'min', 'max', 'range', 'decimal', 'number', ...)
 *  'element_name':
 *  The name attribute of the element the error is for.
1097
1098
1099
1100
1101
1102
 *
 * @return string
 */
function theme_clientside_error($variables) {
  return t($variables['message'], $variables['placeholders']);
}
1103

Peter Droogmans's avatar
Peter Droogmans committed
1104
function clientside_validation_settings_delete($cvs_type, $cvs_formid) {
Peter Droogmans's avatar
Peter Droogmans committed
1105
  db_delete('clientside_validation_settings')
Peter Droogmans's avatar
Peter Droogmans committed
1106
1107
    ->condition('type', $cvs_type)
    ->condition('form_id', $cvs_formid)
Peter Droogmans's avatar
Peter Droogmans committed
1108
1109
1110
    ->execute();
}

Peter Droogmans's avatar
Peter Droogmans committed
1111
function clientside_validation_settings_enable($cvs_type, $cvs_formid) {
Peter Droogmans's avatar
Peter Droogmans committed
1112
1113
  db_update('clientside_validation_settings')
    ->fields(array(
Peter Droogmans's avatar
Peter Droogmans committed
1114
      'status' => 7,
Peter Droogmans's avatar
Peter Droogmans committed
1115
    ))
Peter Droogmans's avatar
Peter Droogmans committed
1116
1117
    ->condition('type', $cvs_type)
    ->condition('form_id', $cvs_formid)
Peter Droogmans's avatar
Peter Droogmans committed
1118
1119
1120
    ->execute();
}

Peter Droogmans's avatar
Peter Droogmans committed
1121
function clientside_validation_settings_disable($cvs_type, $cvs_formid) {
Peter Droogmans's avatar
Peter Droogmans committed
1122
1123
  db_update('clientside_validation_settings')
    ->fields(array(
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
1124
      'status' => 0,
Peter Droogmans's avatar
Peter Droogmans committed
1125
    ))
Peter Droogmans's avatar
Peter Droogmans committed
1126
1127
    ->condition('type', $cvs_type)
    ->condition('form_id', $cvs_formid)
Peter Droogmans's avatar
Peter Droogmans committed
1128
1129
1130
    ->execute();
}

Peter Droogmans's avatar
Peter Droogmans committed
1131
1132
function clientside_validation_settings_add($cvs_type, $cvs_formid, $settings = array(), $enable = FALSE) {
  db_merge('clientside_validation_settings')
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
1133
    ->key(array(
Peter Droogmans's avatar
Peter Droogmans committed
1134
1135
      'type' => $cvs_type,
      'form_id' => $cvs_formid,
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
1136
1137
1138
    ))
    ->fields(array(
      'status' => $enable ? 1 : 0,
1139
1140
1141
1142
1143
      'settings' => serialize($settings),
    ))
    ->execute();
}

Peter Droogmans's avatar
Peter Droogmans committed
1144
function clientside_validation_settings_update($cvs_type, $cvs_formid, $settings) {
1145
1146
1147
1148
  db_update('clientside_validation_settings')
    ->fields(array(
      'settings' => serialize($settings),
    ))
Peter Droogmans's avatar
Peter Droogmans committed
1149
1150
    ->condition('type', $cvs_type)
    ->condition('form_id', $cvs_formid)
1151
1152
1153
    ->execute();
}

Peter Droogmans's avatar
Peter Droogmans committed
1154
function clientside_validation_settings_title($cvs_type, $cvs_formid, $edit = TRUE) {
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
1155
1156
1157
1158
  $title = 'Edit settings of %formid';
  if (!$edit) {
    $title = 'Create settings for %formid';
  }
Peter Droogmans's avatar
Peter Droogmans committed
1159

Peter Droogmans's avatar
Peter Droogmans committed
1160
  switch ($cvs_type) {
Peter Droogmans's avatar
Peter Droogmans committed
1161
1162
1163
    case 'default':
      return t('Edit default settings');
      break;
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
1164
1165
    case 'content-types':
      $content_types = node_type_get_types();
Peter Droogmans's avatar
Peter Droogmans committed
1166
1167
      if (isset($content_types[$cvs_formid])) {
        return t($title, array('%formid' => t($content_types[$cvs_formid]->name)));
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
1168
1169
1170
      }
      break;
    case 'webforms':
Peter Droogmans's avatar
Peter Droogmans committed
1171
      $node = node_load($cvs_formid);
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
1172
1173
1174
1175
1176
      if ($node) {
        return t($title, array('%formid' => $node->title));
      }
      break;
    case 'custom-forms':
Peter Droogmans's avatar
Peter Droogmans committed
1177
      return t($title, array('%formid' => $cvs_formid));
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
1178
1179
      break;
  }
Peter Droogmans's avatar
Peter Droogmans committed
1180
  return t($title, array('%formid' => $cvs_type));
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
1181
1182
}

Peter Droogmans's avatar
Peter Droogmans committed
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
function clientside_validation_settings_current_form($settings = NULL) {
  $current = &drupal_static(__FUNCTION__, array());
  if (isset($settings)) {
    $current = $settings;
    if (isset($current['settings']) && is_array($current['settings'])) {
      $current = $current['settings'];
    }
  }
  return $current;
}

function clientside_validation_settings_load_defaults() {
  static $defaults;
  if (!$defaults) {
Peter Droogmans's avatar
Peter Droogmans committed
1197
    $defaults = clientside_validation_settings_load('default');
Peter Droogmans's avatar
Peter Droogmans committed
1198
1199
1200
1201
  }
  return $defaults;
}

Jelle Sebreghts's avatar
Jelle Sebreghts committed
1202
function clientside_validation_settings_load($cvs_type = NULL, $cvs_formid = NULL, $include_disabled = FALSE) {
Peter Droogmans's avatar
Peter Droogmans committed
1203
  if ($cvs_type == 'default') {
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
    $default_settings = array(
      'validate_options' => array(
        'disable_html5' => 1,
        'validate_onsubmit' => 1,
        'validate_onblur' => 1,
        'validate_onblur_always' => 0,
        'validate_onkeyup' => 1,
        'show_messages' => 0,
      ),
      'error' => array(
        'prefix' => '',
        'suffix' => '',
        'scrollto_errormessage' => 1,
        'scroll_speed' => 1000,
        'error_element' => 'label',
      ),
      'error_placement' => array(
        'error_placement_default' => CLIENTSIDE_VALIDATION_TOP_OF_FORM,
        'jquery_selector' => '',
        'custom_error_function' => '',
      ),
      'include_hidden' => array(
        'include_hidden' => '',
Peter Droogmans's avatar
Peter Droogmans committed
1227
        'include_hidden_fields' => FALSE,
1228
1229
1230
1231
1232
        'validate_tabs' => '',
      ),
    );
    return variable_get('clientside_validation_default_settings', $default_settings);
  }
Peter Droogmans's avatar
Peter Droogmans committed
1233
1234
  $query = db_select('clientside_validation_settings')
    ->fields('clientside_validation_settings');
Peter Droogmans's avatar
Peter Droogmans committed
1235
1236
  if (!empty($cvs_type)) {
    $query->condition('type', $cvs_type);
1237
  }
Peter Droogmans's avatar
Peter Droogmans committed
1238
1239
  if (!empty($cvs_formid)) {
    $query->condition('form_id', $cvs_formid);
1240
  }
Jelle Sebreghts's avatar
Jelle Sebreghts committed
1241
1242
1243
  if (!$include_disabled) {
    $query->condition('status', 1);
  }
1244
1245

  $result = $query->execute();
Peter Droogmans's avatar
upgrade    
Peter Droogmans committed
1246

1247
  // a single id was given -> one result, so only return one row
Peter Droogmans's avatar
Peter Droogmans committed
1248
  if (isset($cvs_formid)) {
Peter Droogmans's avatar
Peter Droogmans committed
1249
    $res = $result->fetchAssoc();
Peter Droogmans's avatar
Peter Droogmans committed
1250
1251
1252
1253
1254
1255
1256
    if ($res) {
      $res['settings'] = unserialize($res['settings']);
      $res['settings'] = drupal_array_merge_deep(clientside_validation_settings_load_defaults(), $res['settings']);
    }
    else {
      $res['settings'] = clientside_validation_settings_load_defaults();
    }
Peter Droogmans's avatar
Peter Droogmans committed
1257
    return $res;
1258
  }
Peter Droogmans's avatar
Peter Droogmans committed
1259
1260
1261
1262
1263
1264
  $res = $result->fetchAllAssoc('form_id');
  if ($res) {
    foreach ($res as &$setting) {
      $setting->settings = unserialize($setting->settings);
      $setting->settings = drupal_array_merge_deep(clientside_validation_settings_load_defaults(), $setting->settings);
    }
1265
  }
Peter Droogmans's avatar
Peter Droogmans committed
1266
  return $res;
1267
}
For faster browsing, not all history is shown. View entire blame