Skip to content

Commit

Permalink
[FEATURE] Add min and max validation for date fields
Browse files Browse the repository at this point in the history
  • Loading branch information
nebrot authored May 16, 2022
1 parent 885ebb1 commit 6106c70
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions Resources/Private/Build/JavaScript/FormValidation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Utility from './Utility';
import moment from 'moment';

export default class FormValidation {
#formValidationSelector = '[data-powermail-validate]';
Expand Down Expand Up @@ -321,15 +322,25 @@ class Form {
return true;
}
let minimum = field.getAttribute('min') || field.getAttribute('data-powermail-min');
return parseInt(field.value) >= parseInt(minimum);
let value = field.value;
if (field.getAttribute('type') === 'date' || field.getAttribute('type') === 'datetime-local' || field.getAttribute('type') === 'time') {
value = this.#getUnixTimestamp(value, field.getAttribute('data-datepicker-format'));
minimum = this.#getUnixTimestamp(minimum, field.getAttribute('data-datepicker-format'));
}
return parseInt(value) >= parseInt(minimum);
};

#isValidationMaximumConfirmed(field) {
if (field.value === '') {
return true;
}
let maximum = field.getAttribute('max') || field.getAttribute('data-powermail-max');
return parseInt(field.value) <= parseInt(maximum);
let value = field.value;
if (field.getAttribute('type') === 'date' || field.getAttribute('type') === 'datetime-local' || field.getAttribute('type') === 'time') {
value = this.#getUnixTimestamp(value, field.getAttribute('data-datepicker-format'));
maximum = this.#getUnixTimestamp(maximum, field.getAttribute('data-datepicker-format'));
}
return parseInt(value) <= parseInt(maximum);
};

#isValidationLengthConfirmed(field) {
Expand Down Expand Up @@ -455,6 +466,14 @@ class Form {
}
return value;
};

#getUnixTimestamp(value, formatInput) {
let momentDate = moment(value, formatInput);
if (momentDate.isValid) {
value = momentDate.unix();
}
return value;
};

#getFieldIdentifier(field) {
let name = field.getAttribute('name');
Expand Down

0 comments on commit 6106c70

Please sign in to comment.