Skip to content

[FEATURE] Add min and max validation for date fields #767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions Resources/Private/Build/JavaScript/FormValidation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Utility from './Utility';
import MoreStepForm from './MoreStepForm';
import moment from 'moment';

export default class FormValidation {
#formValidationSelector = '[data-powermail-validate]';
Expand Down Expand Up @@ -331,15 +332,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 @@ -465,6 +476,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