How to add validation error message on due date using behaviours?

Milankumar Ardeshana
Contributor
August 31, 2018

Trying to force the due date value as mandatory and only accepts today or future date. using behaviours. 

Its still accepting past dates and not throwing error.

 

Following is my script and screenshot is attached.

 

//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 DateField = getFieldByName("due date")
Date DateVal = (Date) DateField.getValue()

// Get todays date
Date now = new Date()


// 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(underlyingIssue.getDueDate().before(now)){
log.debug("This is a debug message")
DateField.setError("You are only allowed to select a Date and time that is greater than 1 hour from now" + "<br/>" + "Your selected date and time 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
DateField.setFormValue(null)
}else{
// If a valid value is entered clear any errors and accept the value
DateField.clearError()
}

 

behabviours script.PNG

1 answer

1 accepted

2 votes
Answer accepted
Milankumar Ardeshana
Contributor
September 5, 2018
//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 DateField = getFieldById("duedate")
//Date DateVal = (Date) DateField.getValue()

// Get todays date
Date now = new Date()
now = now - 1
def fieldValue = (Date) getFieldById("duedate").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")
DateField.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
DateField.setFormValue(null)
}else{
// If a valid value is entered clear any errors and accept the value
DateField.clearError()
}

Suggest an answer

Log in or Sign up to answer