rails4_client_side_validations 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -0
- data/lib/generators/rails4_client_side_validations/copy_assets_generator.rb +58 -0
- data/lib/generators/rails4_client_side_validations/install_generator.rb +22 -0
- data/lib/generators/templates/client_side_validations/initializer.rb +20 -0
- data/lib/rails4_client_side_validations.rb +13 -0
- data/lib/rails4_client_side_validations/action_view.rb +14 -0
- data/lib/rails4_client_side_validations/action_view/form_builder.rb +105 -0
- data/lib/rails4_client_side_validations/action_view/form_helper.rb +139 -0
- data/lib/rails4_client_side_validations/action_view/form_tag_helper.rb +12 -0
- data/lib/rails4_client_side_validations/active_model.rb +143 -0
- data/lib/rails4_client_side_validations/active_model/absence.rb +10 -0
- data/lib/rails4_client_side_validations/active_model/acceptance.rb +10 -0
- data/lib/rails4_client_side_validations/active_model/exclusion.rb +27 -0
- data/lib/rails4_client_side_validations/active_model/format.rb +31 -0
- data/lib/rails4_client_side_validations/active_model/inclusion.rb +26 -0
- data/lib/rails4_client_side_validations/active_model/length.rb +26 -0
- data/lib/rails4_client_side_validations/active_model/numericality.rb +42 -0
- data/lib/rails4_client_side_validations/active_model/presence.rb +10 -0
- data/lib/rails4_client_side_validations/active_record.rb +12 -0
- data/lib/rails4_client_side_validations/active_record/middleware.rb +59 -0
- data/lib/rails4_client_side_validations/active_record/uniqueness.rb +29 -0
- data/lib/rails4_client_side_validations/config.rb +13 -0
- data/lib/rails4_client_side_validations/core_ext.rb +3 -0
- data/lib/rails4_client_side_validations/core_ext/range.rb +10 -0
- data/lib/rails4_client_side_validations/core_ext/regexp.rb +13 -0
- data/lib/rails4_client_side_validations/engine.rb +6 -0
- data/lib/rails4_client_side_validations/files.rb +8 -0
- data/lib/rails4_client_side_validations/generators.rb +12 -0
- data/lib/rails4_client_side_validations/generators/rails_validations.rb +15 -0
- data/lib/rails4_client_side_validations/middleware.rb +120 -0
- data/lib/rails4_client_side_validations/version.rb +3 -0
- data/vendor/assets/javascripts/rails.validations.js +639 -0
- metadata +40 -9
@@ -0,0 +1,639 @@
|
|
1
|
+
(function() {
|
2
|
+
var $, validateElement, validateForm, validatorsFor,
|
3
|
+
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
|
4
|
+
|
5
|
+
$ = jQuery;
|
6
|
+
|
7
|
+
$.fn.disableRails4ClientSideValidations = function() {
|
8
|
+
Rails4Rails4ClientSideValidations.disable(this);
|
9
|
+
return this;
|
10
|
+
};
|
11
|
+
|
12
|
+
$.fn.enableRails4ClientSideValidations = function() {
|
13
|
+
this.filter(Rails4ClientSideValidations.selectors.forms).each(function() {
|
14
|
+
return Rails4ClientSideValidations.enablers.form(this);
|
15
|
+
});
|
16
|
+
this.filter(Rails4ClientSideValidations.selectors.inputs).each(function() {
|
17
|
+
return Rails4ClientSideValidations.enablers.input(this);
|
18
|
+
});
|
19
|
+
return this;
|
20
|
+
};
|
21
|
+
|
22
|
+
$.fn.resetRails4ClientSideValidations = function() {
|
23
|
+
this.filter(Rails4ClientSideValidations.selectors.forms).each(function() {
|
24
|
+
return Rails4ClientSideValidations.reset(this);
|
25
|
+
});
|
26
|
+
return this;
|
27
|
+
};
|
28
|
+
|
29
|
+
$.fn.validate = function() {
|
30
|
+
this.filter(Rails4ClientSideValidations.selectors.forms).each(function() {
|
31
|
+
return $(this).enableRails4ClientSideValidations();
|
32
|
+
});
|
33
|
+
return this;
|
34
|
+
};
|
35
|
+
|
36
|
+
$.fn.isValid = function(validators) {
|
37
|
+
var obj;
|
38
|
+
obj = $(this[0]);
|
39
|
+
if (obj.is('form')) {
|
40
|
+
return validateForm(obj, validators);
|
41
|
+
} else {
|
42
|
+
return validateElement(obj, validatorsFor(this[0].name, validators));
|
43
|
+
}
|
44
|
+
};
|
45
|
+
|
46
|
+
validatorsFor = function(name, validators) {
|
47
|
+
name = name.replace(/_attributes\]\[\w+\]\[(\w+)\]/g, "_attributes][][$1]");
|
48
|
+
return validators[name] || {};
|
49
|
+
};
|
50
|
+
|
51
|
+
validateForm = function(form, validators) {
|
52
|
+
var valid;
|
53
|
+
form.trigger('form:validate:before.Rails4ClientSideValidations');
|
54
|
+
valid = true;
|
55
|
+
form.find(Rails4ClientSideValidations.selectors.validate_inputs).each(function() {
|
56
|
+
if (!$(this).isValid(validators)) {
|
57
|
+
valid = false;
|
58
|
+
}
|
59
|
+
return true;
|
60
|
+
});
|
61
|
+
if (valid) {
|
62
|
+
form.trigger('form:validate:pass.Rails4ClientSideValidations');
|
63
|
+
} else {
|
64
|
+
form.trigger('form:validate:fail.Rails4ClientSideValidations');
|
65
|
+
}
|
66
|
+
form.trigger('form:validate:after.Rails4ClientSideValidations');
|
67
|
+
return valid;
|
68
|
+
};
|
69
|
+
|
70
|
+
validateElement = function(element, validators) {
|
71
|
+
var afterValidate, destroyInputName, executeValidators, failElement, local, passElement, remote;
|
72
|
+
element.trigger('element:validate:before.Rails4ClientSideValidations');
|
73
|
+
passElement = function() {
|
74
|
+
return element.trigger('element:validate:pass.Rails4ClientSideValidations').data('valid', null);
|
75
|
+
};
|
76
|
+
failElement = function(message) {
|
77
|
+
element.trigger('element:validate:fail.Rails4ClientSideValidations', message).data('valid', false);
|
78
|
+
return false;
|
79
|
+
};
|
80
|
+
afterValidate = function() {
|
81
|
+
return element.trigger('element:validate:after.Rails4ClientSideValidations').data('valid') !== false;
|
82
|
+
};
|
83
|
+
executeValidators = function(context) {
|
84
|
+
var fn, kind, message, valid, validator, _i, _len, _ref;
|
85
|
+
valid = true;
|
86
|
+
for (kind in context) {
|
87
|
+
fn = context[kind];
|
88
|
+
if (validators[kind]) {
|
89
|
+
_ref = validators[kind];
|
90
|
+
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
91
|
+
validator = _ref[_i];
|
92
|
+
if (message = fn.call(context, element, validator)) {
|
93
|
+
valid = failElement(message);
|
94
|
+
break;
|
95
|
+
}
|
96
|
+
}
|
97
|
+
if (!valid) {
|
98
|
+
break;
|
99
|
+
}
|
100
|
+
}
|
101
|
+
}
|
102
|
+
return valid;
|
103
|
+
};
|
104
|
+
destroyInputName = element.attr('name').replace(/\[([^\]]*?)\]$/, '[_destroy]');
|
105
|
+
if ($("input[name='" + destroyInputName + "']").val() === "1") {
|
106
|
+
passElement();
|
107
|
+
return afterValidate();
|
108
|
+
}
|
109
|
+
if (element.data('changed') === false) {
|
110
|
+
return afterValidate();
|
111
|
+
}
|
112
|
+
element.data('changed', false);
|
113
|
+
local = Rails4ClientSideValidations.validators.local;
|
114
|
+
remote = Rails4ClientSideValidations.validators.remote;
|
115
|
+
if (executeValidators(local) && executeValidators(remote)) {
|
116
|
+
passElement();
|
117
|
+
}
|
118
|
+
return afterValidate();
|
119
|
+
};
|
120
|
+
|
121
|
+
if (window.Rails4ClientSideValidations === void 0) {
|
122
|
+
window.Rails4ClientSideValidations = {};
|
123
|
+
}
|
124
|
+
|
125
|
+
if (window.Rails4ClientSideValidations.forms === void 0) {
|
126
|
+
window.Rails4ClientSideValidations.forms = {};
|
127
|
+
}
|
128
|
+
|
129
|
+
window.Rails4ClientSideValidations.selectors = {
|
130
|
+
inputs: ':input:not(button):not([type="submit"])[name]:visible:enabled',
|
131
|
+
validate_inputs: ':input:enabled:visible[data-validate]',
|
132
|
+
forms: 'form[data-validate]'
|
133
|
+
};
|
134
|
+
|
135
|
+
window.Rails4ClientSideValidations.reset = function(form) {
|
136
|
+
var $form, key;
|
137
|
+
$form = $(form);
|
138
|
+
Rails4ClientSideValidations.disable(form);
|
139
|
+
for (key in form.Rails4ClientSideValidations.settings.validators) {
|
140
|
+
form.Rails4ClientSideValidations.removeError($form.find("[name='" + key + "']"));
|
141
|
+
}
|
142
|
+
return Rails4ClientSideValidations.enablers.form(form);
|
143
|
+
};
|
144
|
+
|
145
|
+
window.Rails4ClientSideValidations.disable = function(target) {
|
146
|
+
var $target;
|
147
|
+
$target = $(target);
|
148
|
+
$target.off('.Rails4ClientSideValidations');
|
149
|
+
if ($target.is('form')) {
|
150
|
+
return Rails4ClientSideValidations.disable($target.find(':input'));
|
151
|
+
} else {
|
152
|
+
$target.removeData('valid');
|
153
|
+
$target.removeData('changed');
|
154
|
+
return $target.filter(':input').each(function() {
|
155
|
+
return $(this).removeAttr('data-validate');
|
156
|
+
});
|
157
|
+
}
|
158
|
+
};
|
159
|
+
|
160
|
+
window.Rails4ClientSideValidations.enablers = {
|
161
|
+
form: function(form) {
|
162
|
+
var $form, binding, event, _ref;
|
163
|
+
$form = $(form);
|
164
|
+
form.Rails4ClientSideValidations = {
|
165
|
+
settings: window.Rails4ClientSideValidations.forms[$form.attr('id')],
|
166
|
+
addError: function(element, message) {
|
167
|
+
return Rails4ClientSideValidations.formBuilders[form.Rails4ClientSideValidations.settings.type].add(element, form.Rails4ClientSideValidations.settings, message);
|
168
|
+
},
|
169
|
+
removeError: function(element) {
|
170
|
+
return Rails4ClientSideValidations.formBuilders[form.Rails4ClientSideValidations.settings.type].remove(element, form.Rails4ClientSideValidations.settings);
|
171
|
+
}
|
172
|
+
};
|
173
|
+
_ref = {
|
174
|
+
'submit.Rails4ClientSideValidations': function(eventData) {
|
175
|
+
if (!$form.isValid(form.Rails4ClientSideValidations.settings.validators)) {
|
176
|
+
eventData.preventDefault();
|
177
|
+
return eventData.stopImmediatePropagation();
|
178
|
+
}
|
179
|
+
},
|
180
|
+
'ajax:beforeSend.Rails4ClientSideValidations': function(eventData) {
|
181
|
+
if (eventData.target === this) {
|
182
|
+
return $form.isValid(form.Rails4ClientSideValidations.settings.validators);
|
183
|
+
}
|
184
|
+
},
|
185
|
+
'form:validate:after.Rails4ClientSideValidations': function(eventData) {
|
186
|
+
return Rails4ClientSideValidations.callbacks.form.after($form, eventData);
|
187
|
+
},
|
188
|
+
'form:validate:before.Rails4ClientSideValidations': function(eventData) {
|
189
|
+
return Rails4ClientSideValidations.callbacks.form.before($form, eventData);
|
190
|
+
},
|
191
|
+
'form:validate:fail.Rails4ClientSideValidations': function(eventData) {
|
192
|
+
return Rails4ClientSideValidations.callbacks.form.fail($form, eventData);
|
193
|
+
},
|
194
|
+
'form:validate:pass.Rails4ClientSideValidations': function(eventData) {
|
195
|
+
return Rails4ClientSideValidations.callbacks.form.pass($form, eventData);
|
196
|
+
}
|
197
|
+
};
|
198
|
+
for (event in _ref) {
|
199
|
+
binding = _ref[event];
|
200
|
+
$form.on(event, binding);
|
201
|
+
}
|
202
|
+
return $form.find(Rails4ClientSideValidations.selectors.inputs).each(function() {
|
203
|
+
return Rails4ClientSideValidations.enablers.input(this);
|
204
|
+
});
|
205
|
+
},
|
206
|
+
input: function(input) {
|
207
|
+
var $form, $input, binding, event, form, _ref;
|
208
|
+
$input = $(input);
|
209
|
+
form = input.form;
|
210
|
+
$form = $(form);
|
211
|
+
_ref = {
|
212
|
+
'focusout.Rails4ClientSideValidations': function() {
|
213
|
+
return $(this).isValid(form.Rails4ClientSideValidations.settings.validators);
|
214
|
+
},
|
215
|
+
'change.Rails4ClientSideValidations': function() {
|
216
|
+
return $(this).data('changed', true);
|
217
|
+
},
|
218
|
+
'element:validate:after.Rails4ClientSideValidations': function(eventData) {
|
219
|
+
return Rails4ClientSideValidations.callbacks.element.after($(this), eventData);
|
220
|
+
},
|
221
|
+
'element:validate:before.Rails4ClientSideValidations': function(eventData) {
|
222
|
+
return Rails4ClientSideValidations.callbacks.element.before($(this), eventData);
|
223
|
+
},
|
224
|
+
'element:validate:fail.Rails4ClientSideValidations': function(eventData, message) {
|
225
|
+
var element;
|
226
|
+
element = $(this);
|
227
|
+
return Rails4ClientSideValidations.callbacks.element.fail(element, message, function() {
|
228
|
+
return form.Rails4ClientSideValidations.addError(element, message);
|
229
|
+
}, eventData);
|
230
|
+
},
|
231
|
+
'element:validate:pass.Rails4ClientSideValidations': function(eventData) {
|
232
|
+
var element;
|
233
|
+
element = $(this);
|
234
|
+
return Rails4ClientSideValidations.callbacks.element.pass(element, function() {
|
235
|
+
return form.Rails4ClientSideValidations.removeError(element);
|
236
|
+
}, eventData);
|
237
|
+
}
|
238
|
+
};
|
239
|
+
for (event in _ref) {
|
240
|
+
binding = _ref[event];
|
241
|
+
$input.filter(':not(:radio):not([id$=_confirmation])').each(function() {
|
242
|
+
return $(this).attr('data-validate', true);
|
243
|
+
}).on(event, binding);
|
244
|
+
}
|
245
|
+
$input.filter(':checkbox').on('change.Rails4ClientSideValidations', function() {
|
246
|
+
return $(this).isValid(form.Rails4ClientSideValidations.settings.validators);
|
247
|
+
});
|
248
|
+
return $input.filter('[id$=_confirmation]').each(function() {
|
249
|
+
var confirmationElement, element, _ref1, _results;
|
250
|
+
confirmationElement = $(this);
|
251
|
+
element = $form.find("#" + (this.id.match(/(.+)_confirmation/)[1]) + ":input");
|
252
|
+
if (element[0]) {
|
253
|
+
_ref1 = {
|
254
|
+
'focusout.Rails4ClientSideValidations': function() {
|
255
|
+
return element.data('changed', true).isValid(form.Rails4ClientSideValidations.settings.validators);
|
256
|
+
},
|
257
|
+
'keyup.Rails4ClientSideValidations': function() {
|
258
|
+
return element.data('changed', true).isValid(form.Rails4ClientSideValidations.settings.validators);
|
259
|
+
}
|
260
|
+
};
|
261
|
+
_results = [];
|
262
|
+
for (event in _ref1) {
|
263
|
+
binding = _ref1[event];
|
264
|
+
_results.push($("#" + (confirmationElement.attr('id'))).on(event, binding));
|
265
|
+
}
|
266
|
+
return _results;
|
267
|
+
}
|
268
|
+
});
|
269
|
+
}
|
270
|
+
};
|
271
|
+
|
272
|
+
window.Rails4ClientSideValidations.validators = {
|
273
|
+
all: function() {
|
274
|
+
return jQuery.extend({}, Rails4ClientSideValidations.validators.local, Rails4ClientSideValidations.validators.remote);
|
275
|
+
},
|
276
|
+
local: {
|
277
|
+
absence: function(element, options) {
|
278
|
+
if (!/^\s*$/.test(element.val() || '')) {
|
279
|
+
return options.message;
|
280
|
+
}
|
281
|
+
},
|
282
|
+
presence: function(element, options) {
|
283
|
+
if (/^\s*$/.test(element.val() || '')) {
|
284
|
+
return options.message;
|
285
|
+
}
|
286
|
+
},
|
287
|
+
acceptance: function(element, options) {
|
288
|
+
var _ref;
|
289
|
+
switch (element.attr('type')) {
|
290
|
+
case 'checkbox':
|
291
|
+
if (!element.prop('checked')) {
|
292
|
+
return options.message;
|
293
|
+
}
|
294
|
+
break;
|
295
|
+
case 'text':
|
296
|
+
if (element.val() !== (((_ref = options.accept) != null ? _ref.toString() : void 0) || '1')) {
|
297
|
+
return options.message;
|
298
|
+
}
|
299
|
+
}
|
300
|
+
},
|
301
|
+
format: function(element, options) {
|
302
|
+
var message;
|
303
|
+
message = this.presence(element, options);
|
304
|
+
if (message) {
|
305
|
+
if (options.allow_blank === true) {
|
306
|
+
return;
|
307
|
+
}
|
308
|
+
return message;
|
309
|
+
}
|
310
|
+
if (options["with"] && !options["with"].test(element.val())) {
|
311
|
+
return options.message;
|
312
|
+
}
|
313
|
+
if (options.without && options.without.test(element.val())) {
|
314
|
+
return options.message;
|
315
|
+
}
|
316
|
+
},
|
317
|
+
numericality: function(element, options) {
|
318
|
+
var CHECKS, check, check_value, fn, form, operator, val;
|
319
|
+
val = jQuery.trim(element.val());
|
320
|
+
if (!Rails4ClientSideValidations.patterns.numericality.test(val)) {
|
321
|
+
if (options.allow_blank === true && this.presence(element, {
|
322
|
+
message: options.messages.numericality
|
323
|
+
})) {
|
324
|
+
return;
|
325
|
+
}
|
326
|
+
return options.messages.numericality;
|
327
|
+
}
|
328
|
+
if (options.only_integer && !/^[+-]?\d+$/.test(val)) {
|
329
|
+
return options.messages.only_integer;
|
330
|
+
}
|
331
|
+
CHECKS = {
|
332
|
+
greater_than: '>',
|
333
|
+
greater_than_or_equal_to: '>=',
|
334
|
+
equal_to: '==',
|
335
|
+
less_than: '<',
|
336
|
+
less_than_or_equal_to: '<='
|
337
|
+
};
|
338
|
+
form = $(element[0].form);
|
339
|
+
for (check in CHECKS) {
|
340
|
+
operator = CHECKS[check];
|
341
|
+
if (!(options[check] != null)) {
|
342
|
+
continue;
|
343
|
+
}
|
344
|
+
if (!isNaN(parseFloat(options[check])) && isFinite(options[check])) {
|
345
|
+
check_value = options[check];
|
346
|
+
} else if (form.find("[name*=" + options[check] + "]").size() === 1) {
|
347
|
+
check_value = form.find("[name*=" + options[check] + "]").val();
|
348
|
+
} else {
|
349
|
+
return;
|
350
|
+
}
|
351
|
+
val = val.replace(new RegExp("\\" + Rails4ClientSideValidations.number_format.delimiter, 'g'), "").replace(new RegExp("\\" + Rails4ClientSideValidations.number_format.separator, 'g'), ".");
|
352
|
+
fn = new Function("return " + val + " " + operator + " " + check_value);
|
353
|
+
if (!fn()) {
|
354
|
+
return options.messages[check];
|
355
|
+
}
|
356
|
+
}
|
357
|
+
if (options.odd && !(parseInt(val, 10) % 2)) {
|
358
|
+
return options.messages.odd;
|
359
|
+
}
|
360
|
+
if (options.even && (parseInt(val, 10) % 2)) {
|
361
|
+
return options.messages.even;
|
362
|
+
}
|
363
|
+
},
|
364
|
+
length: function(element, options) {
|
365
|
+
var CHECKS, blankOptions, check, fn, message, operator, tokenized_length, tokenizer;
|
366
|
+
tokenizer = options.js_tokenizer || "split('')";
|
367
|
+
tokenized_length = new Function('element', "return (element.val()." + tokenizer + " || '').length")(element);
|
368
|
+
CHECKS = {
|
369
|
+
is: '==',
|
370
|
+
minimum: '>=',
|
371
|
+
maximum: '<='
|
372
|
+
};
|
373
|
+
blankOptions = {};
|
374
|
+
blankOptions.message = options.is ? options.messages.is : options.minimum ? options.messages.minimum : void 0;
|
375
|
+
message = this.presence(element, blankOptions);
|
376
|
+
if (message) {
|
377
|
+
if (options.allow_blank === true) {
|
378
|
+
return;
|
379
|
+
}
|
380
|
+
return message;
|
381
|
+
}
|
382
|
+
for (check in CHECKS) {
|
383
|
+
operator = CHECKS[check];
|
384
|
+
if (!options[check]) {
|
385
|
+
continue;
|
386
|
+
}
|
387
|
+
fn = new Function("return " + tokenized_length + " " + operator + " " + options[check]);
|
388
|
+
if (!fn()) {
|
389
|
+
return options.messages[check];
|
390
|
+
}
|
391
|
+
}
|
392
|
+
},
|
393
|
+
exclusion: function(element, options) {
|
394
|
+
var lower, message, option, upper, _ref;
|
395
|
+
message = this.presence(element, options);
|
396
|
+
if (message) {
|
397
|
+
if (options.allow_blank === true) {
|
398
|
+
return;
|
399
|
+
}
|
400
|
+
return message;
|
401
|
+
}
|
402
|
+
if (options["in"]) {
|
403
|
+
if (_ref = element.val(), __indexOf.call((function() {
|
404
|
+
var _i, _len, _ref1, _results;
|
405
|
+
_ref1 = options["in"];
|
406
|
+
_results = [];
|
407
|
+
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
|
408
|
+
option = _ref1[_i];
|
409
|
+
_results.push(option.toString());
|
410
|
+
}
|
411
|
+
return _results;
|
412
|
+
})(), _ref) >= 0) {
|
413
|
+
return options.message;
|
414
|
+
}
|
415
|
+
}
|
416
|
+
if (options.range) {
|
417
|
+
lower = options.range[0];
|
418
|
+
upper = options.range[1];
|
419
|
+
if (element.val() >= lower && element.val() <= upper) {
|
420
|
+
return options.message;
|
421
|
+
}
|
422
|
+
}
|
423
|
+
},
|
424
|
+
inclusion: function(element, options) {
|
425
|
+
var lower, message, option, upper, _ref;
|
426
|
+
message = this.presence(element, options);
|
427
|
+
if (message) {
|
428
|
+
if (options.allow_blank === true) {
|
429
|
+
return;
|
430
|
+
}
|
431
|
+
return message;
|
432
|
+
}
|
433
|
+
if (options["in"]) {
|
434
|
+
if (_ref = element.val(), __indexOf.call((function() {
|
435
|
+
var _i, _len, _ref1, _results;
|
436
|
+
_ref1 = options["in"];
|
437
|
+
_results = [];
|
438
|
+
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
|
439
|
+
option = _ref1[_i];
|
440
|
+
_results.push(option.toString());
|
441
|
+
}
|
442
|
+
return _results;
|
443
|
+
})(), _ref) >= 0) {
|
444
|
+
return;
|
445
|
+
}
|
446
|
+
return options.message;
|
447
|
+
}
|
448
|
+
if (options.range) {
|
449
|
+
lower = options.range[0];
|
450
|
+
upper = options.range[1];
|
451
|
+
if (element.val() >= lower && element.val() <= upper) {
|
452
|
+
return;
|
453
|
+
}
|
454
|
+
return options.message;
|
455
|
+
}
|
456
|
+
},
|
457
|
+
confirmation: function(element, options) {
|
458
|
+
if (element.val() !== jQuery("#" + (element.attr('id')) + "_confirmation").val()) {
|
459
|
+
return options.message;
|
460
|
+
}
|
461
|
+
},
|
462
|
+
uniqueness: function(element, options) {
|
463
|
+
var form, matches, name, name_prefix, name_suffix, valid, value;
|
464
|
+
name = element.attr('name');
|
465
|
+
if (/_attributes\]\[\d/.test(name)) {
|
466
|
+
matches = name.match(/^(.+_attributes\])\[\d+\](.+)$/);
|
467
|
+
name_prefix = matches[1];
|
468
|
+
name_suffix = matches[2];
|
469
|
+
value = element.val();
|
470
|
+
if (name_prefix && name_suffix) {
|
471
|
+
form = element.closest('form');
|
472
|
+
valid = true;
|
473
|
+
form.find(':input[name^="' + name_prefix + '"][name$="' + name_suffix + '"]').each(function() {
|
474
|
+
if ($(this).attr('name') !== name) {
|
475
|
+
if ($(this).val() === value) {
|
476
|
+
valid = false;
|
477
|
+
return $(this).data('notLocallyUnique', true);
|
478
|
+
} else {
|
479
|
+
if ($(this).data('notLocallyUnique')) {
|
480
|
+
return $(this).removeData('notLocallyUnique').data('changed', true);
|
481
|
+
}
|
482
|
+
}
|
483
|
+
}
|
484
|
+
});
|
485
|
+
if (!valid) {
|
486
|
+
return options.message;
|
487
|
+
}
|
488
|
+
}
|
489
|
+
}
|
490
|
+
}
|
491
|
+
},
|
492
|
+
remote: {
|
493
|
+
uniqueness: function(element, options) {
|
494
|
+
var data, key, message, name, scope_value, scoped_element, scoped_name, _ref;
|
495
|
+
message = Rails4ClientSideValidations.validators.local.presence(element, options);
|
496
|
+
if (message) {
|
497
|
+
if (options.allow_blank === true) {
|
498
|
+
return;
|
499
|
+
}
|
500
|
+
return message;
|
501
|
+
}
|
502
|
+
data = {};
|
503
|
+
data.case_sensitive = !!options.case_sensitive;
|
504
|
+
if (options.id) {
|
505
|
+
data.id = options.id;
|
506
|
+
}
|
507
|
+
if (options.scope) {
|
508
|
+
data.scope = {};
|
509
|
+
_ref = options.scope;
|
510
|
+
for (key in _ref) {
|
511
|
+
scope_value = _ref[key];
|
512
|
+
scoped_name = element.attr('name').replace(/\[\w+\]$/, "[" + key + "]");
|
513
|
+
scoped_element = jQuery("[name='" + scoped_name + "']");
|
514
|
+
jQuery("[name='" + scoped_name + "']:checkbox").each(function() {
|
515
|
+
if (this.checked) {
|
516
|
+
return scoped_element = this;
|
517
|
+
}
|
518
|
+
});
|
519
|
+
if (scoped_element[0] && scoped_element.val() !== scope_value) {
|
520
|
+
data.scope[key] = scoped_element.val();
|
521
|
+
scoped_element.unbind("change." + element.id).bind("change." + element.id, function() {
|
522
|
+
element.trigger('change.Rails4ClientSideValidations');
|
523
|
+
return element.trigger('focusout.Rails4ClientSideValidations');
|
524
|
+
});
|
525
|
+
} else {
|
526
|
+
data.scope[key] = scope_value;
|
527
|
+
}
|
528
|
+
}
|
529
|
+
}
|
530
|
+
if (/_attributes\]/.test(element.attr('name'))) {
|
531
|
+
name = element.attr('name').match(/\[\w+_attributes\]/g).pop().match(/\[(\w+)_attributes\]/).pop();
|
532
|
+
name += /(\[\w+\])$/.exec(element.attr('name'))[1];
|
533
|
+
} else {
|
534
|
+
name = element.attr('name');
|
535
|
+
}
|
536
|
+
if (options['class']) {
|
537
|
+
name = options['class'] + '[' + name.split('[')[1];
|
538
|
+
}
|
539
|
+
data[name] = element.val();
|
540
|
+
if (Rails4ClientSideValidations.remote_validators_prefix == null) {
|
541
|
+
Rails4ClientSideValidations.remote_validators_prefix = "";
|
542
|
+
}
|
543
|
+
if (jQuery.ajax({
|
544
|
+
url: "" + Rails4ClientSideValidations.remote_validators_prefix + "/validators/uniqueness",
|
545
|
+
data: data,
|
546
|
+
async: false,
|
547
|
+
cache: false
|
548
|
+
}).status === 200) {
|
549
|
+
return options.message;
|
550
|
+
}
|
551
|
+
}
|
552
|
+
}
|
553
|
+
};
|
554
|
+
|
555
|
+
window.Rails4ClientSideValidations.disableValidators = function() {
|
556
|
+
var func, validator, _ref, _results;
|
557
|
+
if (window.Rails4ClientSideValidations.disabled_validators === void 0) {
|
558
|
+
return;
|
559
|
+
}
|
560
|
+
_ref = window.Rails4ClientSideValidations.validators.remote;
|
561
|
+
_results = [];
|
562
|
+
for (validator in _ref) {
|
563
|
+
func = _ref[validator];
|
564
|
+
if (window.Rails4ClientSideValidations.disabled_validators.indexOf(validator) !== -1) {
|
565
|
+
_results.push(delete window.Rails4ClientSideValidations.validators.remote[validator]);
|
566
|
+
} else {
|
567
|
+
_results.push(void 0);
|
568
|
+
}
|
569
|
+
}
|
570
|
+
return _results;
|
571
|
+
};
|
572
|
+
|
573
|
+
window.Rails4ClientSideValidations.formBuilders = {
|
574
|
+
'ActionView::Helpers::FormBuilder': {
|
575
|
+
add: function(element, settings, message) {
|
576
|
+
var form, inputErrorField, label, labelErrorField;
|
577
|
+
form = $(element[0].form);
|
578
|
+
if (element.data('valid') !== false && (form.find("label.message[for='" + (element.attr('id')) + "']")[0] == null)) {
|
579
|
+
inputErrorField = jQuery(settings.input_tag);
|
580
|
+
labelErrorField = jQuery(settings.label_tag);
|
581
|
+
label = form.find("label[for='" + (element.attr('id')) + "']:not(.message)");
|
582
|
+
if (element.attr('autofocus')) {
|
583
|
+
element.attr('autofocus', false);
|
584
|
+
}
|
585
|
+
element.before(inputErrorField);
|
586
|
+
inputErrorField.find('span#input_tag').replaceWith(element);
|
587
|
+
inputErrorField.find('label.message').attr('for', element.attr('id'));
|
588
|
+
labelErrorField.find('label.message').attr('for', element.attr('id'));
|
589
|
+
labelErrorField.insertAfter(label);
|
590
|
+
labelErrorField.find('label#label_tag').replaceWith(label);
|
591
|
+
}
|
592
|
+
return form.find("label.message[for='" + (element.attr('id')) + "']").text(message);
|
593
|
+
},
|
594
|
+
remove: function(element, settings) {
|
595
|
+
var errorFieldClass, form, inputErrorField, label, labelErrorField;
|
596
|
+
form = $(element[0].form);
|
597
|
+
errorFieldClass = jQuery(settings.input_tag).attr('class');
|
598
|
+
inputErrorField = element.closest("." + (errorFieldClass.replace(" ", ".")));
|
599
|
+
label = form.find("label[for='" + (element.attr('id')) + "']:not(.message)");
|
600
|
+
labelErrorField = label.closest("." + errorFieldClass);
|
601
|
+
if (inputErrorField[0]) {
|
602
|
+
inputErrorField.find("#" + (element.attr('id'))).detach();
|
603
|
+
inputErrorField.replaceWith(element);
|
604
|
+
label.detach();
|
605
|
+
return labelErrorField.replaceWith(label);
|
606
|
+
}
|
607
|
+
}
|
608
|
+
}
|
609
|
+
};
|
610
|
+
|
611
|
+
window.Rails4ClientSideValidations.patterns = {
|
612
|
+
numericality: /^(-|\+)?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d*)?$/
|
613
|
+
};
|
614
|
+
|
615
|
+
window.Rails4ClientSideValidations.callbacks = {
|
616
|
+
element: {
|
617
|
+
after: function(element, eventData) {},
|
618
|
+
before: function(element, eventData) {},
|
619
|
+
fail: function(element, message, addError, eventData) {
|
620
|
+
return addError();
|
621
|
+
},
|
622
|
+
pass: function(element, removeError, eventData) {
|
623
|
+
return removeError();
|
624
|
+
}
|
625
|
+
},
|
626
|
+
form: {
|
627
|
+
after: function(form, eventData) {},
|
628
|
+
before: function(form, eventData) {},
|
629
|
+
fail: function(form, eventData) {},
|
630
|
+
pass: function(form, eventData) {}
|
631
|
+
}
|
632
|
+
};
|
633
|
+
|
634
|
+
$(function() {
|
635
|
+
Rails4ClientSideValidations.disableValidators();
|
636
|
+
return $(Rails4ClientSideValidations.selectors.forms).validate();
|
637
|
+
});
|
638
|
+
|
639
|
+
}).call(this);
|