formio.js
formio.js copied to clipboard
data grid date validation
I have no idea how to do this on form.io form builder.
I have a datagrid that collect user's employment history.
I want to validate each row if the end date is greater than the start date,
And validate all of the dates if total years of experience is at least 3 years.
This is I believe that what you were expecting i think, here is what i did
- I disabled all dates that are before the start date using a custom function.
- I added a validation rule to ensure the end date is greater than the start date.
- Inside the Edit Grid, I calculated the total years of experience based on all entries.
- I added a validation to check if the total experience is less than 3 years, and display an error accordingly.
{
"label": "Edit Grid",
"tableView": false,
"validate": {
"custom": "let totalMonths = 0;\r\n\r\nif (data.employmentHistory && Array.isArray(data.employmentHistory)) {\r\n data.employmentHistory.forEach(row => {\r\n const start = moment(row.startDate, 'YYYY-MM-DD');\r\n const end = moment(row.endDate, 'YYYY-MM-DD');\r\n if (start.isValid() && end.isValid() && end.isAfter(start)) {\r\n const duration = end.diff(start, 'months', true);\r\n totalMonths += duration;\r\n }\r\n });\r\n}\r\n\r\nconst totalYears = totalMonths / 12;\r\nif (totalYears < 3) {\r\n valid = 'Total experience must be at least 3 years.';\r\n} else {\r\n valid = true;\r\n}\r\n"
},
"validateWhenHidden": false,
"rowDrafts": false,
"key": "employmentHistory",
"type": "editgrid",
"displayAsTable": true,
"input": true,
"components": [
{
"label": "Company Name",
"applyMaskOn": "change",
"tableView": true,
"validateWhenHidden": false,
"key": "employeeName",
"type": "textfield",
"input": true
},
{
"label": "Start Date",
"tableView": false,
"datePicker": {
"disableWeekends": false,
"disableWeekdays": false
},
"enableMinDateInput": false,
"enableMaxDateInput": false,
"validateWhenHidden": false,
"key": "startDate",
"type": "datetime",
"input": true,
"widget": {
"type": "calendar",
"displayInTimezone": "viewer",
"locale": "en",
"useLocaleSettings": false,
"allowInput": true,
"mode": "single",
"enableTime": true,
"noCalendar": false,
"format": "yyyy-MM-dd hh:mm a",
"hourIncrement": 1,
"minuteIncrement": 1,
"time_24hr": false,
"minDate": null,
"disableWeekends": false,
"disableWeekdays": false,
"maxDate": null
}
},
{
"label": "End Date",
"tableView": false,
"datePicker": {
"disableFunction": "moment(date).isBefore(row.startDate)",
"disableWeekends": false,
"disableWeekdays": false
},
"validate": {
"custom": "const start = moment(row.startDate, 'YYYY-MM-DD');\r\nconst end = moment(input, 'YYYY-MM-DD');\r\n\r\nif (!start.isValid() || !end.isValid()) {\r\n valid = true; \r\n} else if (!end.isAfter(start)) {\r\n valid = 'End date must be after start date.';\r\n} else {\r\n valid = true;\r\n}\r\n"
},
"enableMinDateInput": false,
"enableMaxDateInput": false,
"validateWhenHidden": false,
"key": "endDate",
"type": "datetime",
"input": true,
"widget": {
"type": "calendar",
"displayInTimezone": "viewer",
"locale": "en",
"useLocaleSettings": false,
"allowInput": true,
"mode": "single",
"enableTime": true,
"noCalendar": false,
"format": "yyyy-MM-dd hh:mm a",
"hourIncrement": 1,
"minuteIncrement": 1,
"time_24hr": false,
"minDate": null,
"disableWeekends": false,
"disableWeekdays": false,
"disableFunction": "moment(date).isBefore(row.startDate)",
"maxDate": null
}
}
]
}
Thanks for the contribution @seeranjeeviramavel I'm going to close this as resolved.