Prevent users from entering a date in the past

Bill Davis October 31, 2011

We have created a custom date field and we want to prevent users from selecting a date in the prior month or even a date in the past.

Is there a standard way of achieving this or is there a plug-in that is available.

Any help would be appreciated...thanks!

2 answers

1 accepted

0 votes
Answer accepted
Jobin Kuruvilla [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 31, 2011

If you are using this field only on workflow transitions, you can use some of the existing validators.

But if you want this field on the edit screens, you would need a new custom field type that does this validation. That will be a new plugin I guess.

Bill Davis November 1, 2011

Do you have the name of the existing validator that may work, all I see for Validators is Date Compare and Window dates that relate to the dates. Is it one of those?

Jobin Kuruvilla [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 1, 2011

I don't remember which one it was. Can you try Date Compare and validate your Date field is after updated date?

Bill Davis November 2, 2011

Thanks Jobin, we got the Date Compare to work for us.

1 vote
Dieter
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 1, 2011

If you want to use the Groovy Runner Script plugin to validate you could use this code which validates that the entered date/time is from this month or later. I'm not sure if existing plugins can check this exactly

import org.apache.log4j.Category
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.opensymphony.workflow.InvalidInputException
 
// replace 10000 by number of date time custom field that must be validated 
def fieldname = "customfield_10000"

ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
CustomField field = customFieldManager.getCustomFieldObject(fieldname)
 
Date value = issue.getCustomFieldValue(field)
Calendar now = Calendar.getInstance()
Calendar entered_date = Calendar.getInstance()
entered_date.setTime value

if (entered_date.get(Calendar.MONTH) == now.get(Calendar.MONTH) && entered_date.get(Calendar.YEAR) == now.get(Calendar.YEAR))
	return;
	
if (now.before(entered_date))
	return;
	
invalidInputException = new InvalidInputException("Entered date is invalid! Must be from this month or later")

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events