I want to add the following validation. I have two dates, StartDate and EndDate, how can I display a validation message if the difference between the end date and the start date is not equal to 2? Specifically, I want this error to be displayed every time 2 dates are added and the difference between the end date and the start date is not 2 hours and only when the difference between the two is 2 hours can the event be created from editor template modal.
index.js:
export class EditorFieldValidation extends SampleBase { scheduleObj; scheduleStartTime; scheduleEndTime; data = extend([], dataSource.scheduleData, null, true); fields = { subject: { name: 'Subject', validation: { required: true } }, location: { name: 'Location', validation: { required: true, regex: [ '^[a-zA-Z0-9- ]*$', 'Special characters are not allowed in this field', ], }, }, description: { name: 'Description', validation: { required: true, minLength: 5, maxLength: 500, }, }, startTime: { name: 'StartTime', validation: { required: true, }, }, endTime: { name: 'EndTime', validation: { required: true, range: [ this.customEndFn, 'Difference between the end date and the start date is not 2 hours', ], }, }, }; customEndFn(args) { this.scheduleStartTime = new Date( document.querySelector('#StartTime').value ).getTime(); this.scheduleEndTime = new Date(args.element.value).getTime(); return this.scheduleEndTime - this.scheduleStartTime === 7200000; } onEventRendered(args) { applyCategoryColor(args, this.scheduleObj.currentView); } render() { return ( <div className="schedule-control-section"> <div className="col-lg-12 control-section"> <div className="control-wrapper"> <ScheduleComponent width="100%" height="550px" selectedDate={new Date(2021, 0, 10)} ref={(t) => (this.scheduleObj = t)} eventSettings={{ dataSource: this.data, fields: this.fields }} eventRendered={this.onEventRendered.bind(this)} > <Inject services={[Day, Week, WorkWeek, Month, Resize, DragAndDrop]} /> </ScheduleComponent> </div> </div> </div> ); } } |
It works very well. Thank you :)