forked from Meteor-Community-Packages/meteor-autoform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoform-validation.js
131 lines (116 loc) · 4.15 KB
/
autoform-validation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
_validateForm = function _validateForm(formId, formDetails, formDocs, useCollectionSchema) {
if (formDetails.validationType === 'none')
return true;
// We use the schema for the `schema` attribute if present,
// else the schema for the collection. If there is a `schema`
// attribute but you want to force validation against the
// collection's schema instead, pass useCollectionSchema=true
var ss = (useCollectionSchema && formDetails.collection) ? formDetails.collection.simpleSchema() : formDetails.ss;
// Perform validation
if (formDetails.submitType === "update") {
// For a type="update" form, we validate the modifier. We don't want to throw
// errors about missing required fields, etc.
return validateFormDoc(formDocs.updateDoc, true, formId, ss);
} else {
// For any other type of form, we validate the document.
return validateFormDoc(formDocs.insertDoc, false, formId, ss);
}
};
validateFormDoc = function validateFormDoc(doc, isModifier, formId, ss, key) {
var ec = {
userId: (Meteor.userId && Meteor.userId()) || null,
isInsert: !isModifier,
isUpdate: !!isModifier,
isUpsert: false,
isFromTrustedCode: false
};
// Get a version of the doc that has auto values to validate here. We
// don't want to actually send any auto values to the server because
// we ultimately want them generated on the server
var docForValidation = ss.clean(_.clone(doc), {
isModifier: isModifier,
filter: false,
autoConvert: false,
extendAutoValueContext: ec
});
// Validate
// If `key` is provided, we validate that key/field only
if (key) {
return ss.namedContext(formId).validateOne(docForValidation, key, {
modifier: isModifier,
extendedCustomContext: ec
});
} else {
return ss.namedContext(formId).validate(docForValidation, {
modifier: isModifier,
extendedCustomContext: ec
});
}
};
_validateField = function _validateField(key, template, skipEmpty, onlyIfAlreadyInvalid) {
if (!template || template._notInDOM) {
return; //skip validation
}
var context = template.data;
var formId = context.id || defaultFormId;
var formDetails = formData[formId];
var ss = formDetails.ss;
if (onlyIfAlreadyInvalid && ss.namedContext(formId).isValid()) {
return; //skip validation
}
// Create a document based on all the values of all the inputs on the form
var formDocs = getFormValues(template, formId, ss);
// Clean and validate doc
if (formDetails.submitType === "update") {
var docToValidate = formDocs.updateDoc;
var isModifier = true;
} else {
var docToValidate = formDocs.insertDoc;
var isModifier = false;
}
// Skip validation if skipEmpty is true and the field we're validating
// has no value.
if (skipEmpty && !Utility.objAffectsKey(docToValidate, key))
return true; //skip validation
return validateFormDoc(docToValidate, isModifier, formId, ss, key);
};
//throttling function that calls out to _validateField
var vok = {}, tm = {}, _prevent = false;
validateField = function validateField(key, template, skipEmpty, onlyIfAlreadyInvalid) {
if (vok[key] === false) {
Meteor.clearTimeout(tm[key]);
tm[key] = Meteor.setTimeout(function() {
vok[key] = true;
if (!_prevent) {
_validateField(key, template, skipEmpty, onlyIfAlreadyInvalid);
}
}, 300);
return;
}
vok[key] = false;
if (!_prevent) {
_validateField(key, template, skipEmpty, onlyIfAlreadyInvalid);
}
};
// To prevent issues with keyup validation firing right after we've
// invalidated due to submission, we can quickly and temporarily stop
// field validation.
preventQueuedValidation = function preventQueuedValidation() {
_prevent = true;
Meteor.setTimeout(function() {
_prevent = false;
}, 500);
};
// Prep function to select the focus the first field with an error
selectFirstInvalidField = function selectFirstInvalidField(formId, ss, template) {
var ctx = ss.namedContext(formId);
if (!ctx.isValid()) {
template.$('[data-schema-key]').each(function () {
var f = $(this);
if (ctx.keyIsInvalid(f.attr('data-schema-key'))) {
f.focus();
return false;
}
});
}
};