Validate Custom field( Date & Time) with present time

Sunil Kumar April 13, 2020

Hi,

 

I have a date and time field where i dont want the user to select the date from past. I am using the below script to validate the date and time but it is not working as expexted.

 

Script:

 

//Required Imports
import groovy.time.TimeCategory
import java.util.Date
log.debug("This is a debug message")
// Get a pointer to my Date Custom field
def DateTimeField = getFieldById("Change Start / Time")
//Date DateVal = (Date) DateField.getValue()

// Get todays date
Date now = new Date()
now = now - 1
def fieldValue = (Date) getFieldById("Change Start / Time").value

// Check if the Start Date is before todays date plus 1 hour and if so present the user with an error and clear the value to the field to force the user to enter in a correct value.
if(fieldValue.before(now)){
log.debug("This is a debug message")
DateTimeField.setError("You can't select past date. Your selected date has been cleared to prompt you to select a valid value")
// Clear the weekday value to make the user re select a new value as well
DateTimeField.setFormValue(null)
}else{
// If a valid value is entered clear any errors and accept the value
DateTimeField.clearError()
}

 

Regards,

Sunil

0 answers

Suggest an answer

Log in or Sign up to answer