Skip to content

Commit 77d3410

Browse files
committed
maint(pat validation): Modernize code.
1 parent bcd0aa2 commit 77d3410

File tree

1 file changed

+63
-66
lines changed

1 file changed

+63
-66
lines changed

src/pat/validation/validation.js

Lines changed: 63 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,15 @@ export default Base.extend({
100100
},
101101
// Input is a unix timestamp
102102
format: function (value, options) {
103-
var format = options.dateOnly ? "YYYY-MM-DD" : "YYYY-MM-DD hh:mm:ss";
103+
const format = options.dateOnly ? "YYYY-MM-DD" : "YYYY-MM-DD hh:mm:ss";
104104
return Moment.utc(value).format(format);
105105
},
106106
});
107107
},
108108

109-
getFieldType: function (input) {
110-
var opts = parser.parse($(input));
111-
var type = input.getAttribute("type");
109+
getFieldType(input) {
110+
const opts = parser.parse($(input));
111+
let type = input.getAttribute("type");
112112
if (_.contains(["datetime", "date"], opts.type)) {
113113
type = opts.type;
114114
}
@@ -118,33 +118,33 @@ export default Base.extend({
118118
return type;
119119
},
120120

121-
setLocalDateConstraints: function (input, opts, constraints) {
121+
setLocalDateConstraints(input, opts, constraints) {
122122
/* Set the relative date constraints, i.e. not-after and not-before, as well as custom messages.
123123
*/
124-
var name = input.getAttribute("name").replace(/\./g, "\\.");
125-
var type = this.getFieldType(input);
126-
var c = constraints[name][type];
124+
const name = input.getAttribute("name").replace(/\./g, "\\.");
125+
const type = this.getFieldType(input);
126+
const c = constraints[name][type];
127127

128128
if (!c || typeof opts == "undefined") {
129129
return constraints;
130130
}
131131

132132
_.each(["before", "after"], function (relation) {
133-
var relative = opts.not ? opts.not[relation] : undefined;
134-
var $ref;
133+
const relative = opts.not ? opts.not[relation] : undefined;
135134
if (typeof relative === "undefined") {
136135
return;
137136
}
138-
var relative_constraint = relation === "before" ? "earliest" : "latest";
137+
const relative_constraint = relation === "before" ? "earliest" : "latest";
139138
if (Validate.moment.isDate(relative)) {
140139
c[relative_constraint] = relative;
141140
} else {
141+
let $ref;
142142
try {
143143
$ref = $(relative);
144144
} catch (e) {
145145
console.log(e);
146146
}
147-
var arr = $ref.data("pat-validation-refs") || [];
147+
const arr = $ref.data("pat-validation-refs") || [];
148148
if (!_.contains(arr, input)) {
149149
arr.unshift(input);
150150
$ref.data("pat-validation-refs", arr);
@@ -158,29 +158,26 @@ export default Base.extend({
158158
return constraints;
159159
},
160160

161-
setLocalConstraints: function (input, constraints) {
161+
setLocalConstraints(input, constraints) {
162162
/* Some form fields might have their own data-pat-validation
163163
* attribute, used to set field-specific constraints.
164164
*
165165
* We parse them and add them to the passed in constraints obj.
166166
*/
167-
var name = input.getAttribute("name").replace(/\./g, "\\."),
168-
type = this.getFieldType(input),
169-
opts = parser.parse($(input)),
170-
constraint = constraints[name];
167+
const name = input.getAttribute("name").replace(/\./g, "\\.");
168+
const type = this.getFieldType(input);
169+
const opts = parser.parse($(input));
170+
let constraint = constraints[name];
171171
if (_.contains(["datetime", "date"], type)) {
172172
constraints = this.setLocalDateConstraints(input, opts, constraints);
173173
} else if (type == "number") {
174174
_.each(["min", "max"], function (limit) {
175175
// TODO: need to figure out how to add local validation
176176
// messages for numericality operators
177177
if (input.getAttribute(limit)) {
178-
var constraint = constraints[name],
179-
key =
180-
limit == "min"
181-
? "greaterThanOrEqualTo"
182-
: "lessThanOrEqualTo",
183-
value = Number(input.getAttribute(limit));
178+
const key =
179+
limit == "min" ? "greaterThanOrEqualTo" : "lessThanOrEqualTo";
180+
const value = Number(input.getAttribute(limit));
184181
if (typeof constraint.numericality === "boolean") {
185182
constraint.numericality = {};
186183
}
@@ -209,7 +206,7 @@ export default Base.extend({
209206

210207
// Set local validation messages.
211208
_.each(Object.keys(VALIDATION_TYPE_MAP), function (type) {
212-
var c = constraints[name][VALIDATION_TYPE_MAP[type]];
209+
let c = constraints[name][VALIDATION_TYPE_MAP[type]];
213210
if (c === false) {
214211
c = { message: "^" + opts.message[type] };
215212
} else {
@@ -219,11 +216,11 @@ export default Base.extend({
219216
return constraints;
220217
},
221218

222-
getConstraints: function (input) {
219+
getConstraints(input) {
223220
// Get validation constraints by parsing the input element for hints
224-
var name = input.getAttribute("name"),
225-
type = this.getFieldType(input),
226-
constraints = {};
221+
const name = input.getAttribute("name");
222+
const type = this.getFieldType(input);
223+
const constraints = {};
227224
if (!name) {
228225
return;
229226
}
@@ -246,18 +243,17 @@ export default Base.extend({
246243
? { message: "^" + this.options.message.date }
247244
: false,
248245
};
249-
constraints = this.setLocalConstraints(input, constraints);
250-
return constraints;
246+
return this.setLocalConstraints(input, constraints);
251247
},
252248

253-
doDateCheck: function (input) {
249+
doDateCheck(input) {
254250
// Returns true if a date check should be done.
255251
// Don't check if there is no input - this should be handled by
256252
// the ``required`` attribute.
257253
// In case of HTML5 date/datetime-local support we also have to
258254
// check for ``badInput`` as invalid date input will result in an
259255
// empty ``value``.
260-
var type = input.getAttribute("type"); // we need the raw type here
256+
const type = input.getAttribute("type"); // we need the raw type here
261257
if (
262258
utils.checkInputSupport("date", "wrong value") &&
263259
type.indexOf("date") === 0 &&
@@ -273,13 +269,13 @@ export default Base.extend({
273269
}
274270
},
275271

276-
getValueDict: function (input) {
272+
getValueDict(input) {
277273
/* Return a dict {name: value} derived from a DOM input element.
278274
* Used by validate.js's validate method.
279275
*/
280-
var value_dict = {};
281-
var name = input.getAttribute("name");
282-
var value = input.value;
276+
const value_dict = {};
277+
const name = input.getAttribute("name");
278+
let value = input.value;
283279
if (input.getAttribute("type") == "number") {
284280
if (value !== "") {
285281
try {
@@ -295,23 +291,23 @@ export default Base.extend({
295291
return value_dict;
296292
},
297293

298-
validateForm: function (ev) {
294+
validateForm(ev) {
299295
/* Handler which gets called when the entire form needs to be
300296
* validated. Will prevent the event's default action if validation fails.
301297
*/
302-
var has_errors = false;
298+
let has_errors = false;
303299
// Ignore invisible elements (otherwise pat-clone template
304300
// elements get validated). Not aware of other cases where this
305301
// might cause problems.
306-
var $single = this.$inputs.filter(
302+
const $single = this.$inputs.filter(
307303
":visible:enabled:not(:checkbox):not(:radio), .pat-autosuggest:not(:visible)"
308304
);
309-
var group_names = this.$inputs
305+
const group_names = this.$inputs
310306
.filter(":enabled:checkbox, :enabled:radio")
311307
.map(function () {
312308
return this.getAttribute("name");
313309
});
314-
var handleError = function (error) {
310+
const handleError = function (error) {
315311
if (typeof error != "undefined") {
316312
if (!has_errors && ev) {
317313
ev.preventDefault();
@@ -332,13 +328,13 @@ export default Base.extend({
332328
}
333329
},
334330

335-
customizeMessage: function (msg, input) {
331+
customizeMessage(msg, input) {
336332
/* Due to a limitation in validate.js, whereby we cannot have more
337333
* fine-grained error messages for sub-validations (e.g. is a
338334
* number and is bigger than 5), we need to customize the messages
339335
* after validation. We do that here.
340336
*/
341-
var opts = parser.parse($(input));
337+
const opts = parser.parse($(input));
342338
if (msg.indexOf("must be greater than or equal to") != -1) {
343339
return Validate.format(opts.message.min, {
344340
count: input.getAttribute("min"),
@@ -355,10 +351,10 @@ export default Base.extend({
355351
return msg;
356352
},
357353

358-
validateGroupedElement: function (name) {
354+
validateGroupedElement(name) {
359355
/* Handler which gets called for :checkbox and :radio elments. */
360-
var input = this.$el.find('[name="' + name + '"]')[0];
361-
var error = Validate(
356+
const input = this.$el.find('[name="' + name + '"]')[0];
357+
const error = Validate(
362358
_.pick(Validate.collectFormValues(this.$el), name),
363359
this.getConstraints(input)
364360
);
@@ -375,19 +371,19 @@ export default Base.extend({
375371
return error;
376372
},
377373

378-
validateElement: function (input, no_recurse) {
374+
validateElement(input, no_recurse) {
379375
/* Handler which gets called when a single form input element
380376
* needs to be validated. Will prevent the event's default action
381377
* if validation fails.
382378
*/
383379
if (input.disabled) {
384380
return;
385381
}
386-
var error = Validate(this.getValueDict(input), this.getConstraints(input));
382+
const error = Validate(this.getValueDict(input), this.getConstraints(input));
387383
if (!error) {
388384
this.removeError(input);
389385
} else {
390-
var name = input.getAttribute("name").replace(/\./g, "\\.");
386+
const name = input.getAttribute("name").replace(/\./g, "\\.");
391387
_.each(
392388
error[name],
393389
function (msg) {
@@ -404,7 +400,7 @@ export default Base.extend({
404400
return error;
405401
},
406402

407-
onPatternUpdate: function (ev, data) {
403+
onPatternUpdate(ev, data) {
408404
/* Handler which gets called when pat-update is triggered within
409405
* the .pat-validation element.
410406
*
@@ -424,19 +420,21 @@ export default Base.extend({
424420
return true;
425421
},
426422

427-
findErrorMessages: function (el) {
428-
var $el = $(el),
429-
selector = "em.validation.message",
430-
$messages = $el.next(selector);
423+
findErrorMessages(el) {
424+
const $el = $(el);
425+
const selector = "em.validation.message";
426+
let $messages = $el.next(selector);
431427
if ($el.is("[type=radio],[type=checkbox]")) {
432-
var $fieldset = $el.closest("fieldset.pat-checklist");
433-
if ($fieldset.length) $messages = $fieldset.find(selector);
428+
const $fieldset = $el.closest("fieldset.pat-checklist");
429+
if ($fieldset.length) {
430+
$messages = $fieldset.find(selector);
431+
}
434432
}
435433
return $messages;
436434
},
437435

438-
removeError: function (input) {
439-
var $errors = this.findErrorMessages(input);
436+
removeError(input) {
437+
const $errors = this.findErrorMessages(input);
440438
this.errors = this.errors - $errors.length;
441439
$errors.remove();
442440
utils.findRelatives(input).removeClass("is-invalid").addClass("is-valid");
@@ -447,15 +445,14 @@ export default Base.extend({
447445
}
448446
},
449447

450-
showError: function (error, input) {
451-
var $el = $(input),
452-
$relatives = utils.findRelatives(input),
453-
$position = $el,
454-
strategy = "after",
455-
$message = $("<em/>", { class: "validation warning message" }),
456-
$fieldset;
448+
showError(error, input) {
449+
const $el = $(input);
450+
const $relatives = utils.findRelatives(input);
451+
let $position = $el;
452+
let strategy = "after";
453+
const $message = $("<em/>", { class: "validation warning message" });
457454
if ($el.is("[type=radio],[type=checkbox]")) {
458-
$fieldset = $el.closest("fieldset.pat-checklist");
455+
const $fieldset = $el.closest("fieldset.pat-checklist");
459456
if ($fieldset.length) {
460457
$position = $fieldset;
461458
strategy = "append";

0 commit comments

Comments
 (0)