Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to regex validate a custom field only if the custom field becomes required? Edited

Thank You in advance! I'm trying to validate the regular expression of a single line custom text field (Incident/s), but only when it becomes required.

Background:

During the Defect create validator, I have a ScriptRunner Behavior on the custom field (Incident/s) to make it required only if the custom field (Found Through) has a value that equals (Production). Then only the custom field (Incident/s) becomes required.

Script:

def field1 = getFieldByName("Incident/s")
def field2 = getFieldByName("Found Through")
def field = getFieldByName("Incident/s")
def val = field.getValue() as String

if(field2.getValue().equals("Production"))
{ field1.setRequired(true) }
else { field1.setRequired(false) }

Goal:

Validate the regex of the single line text field (Incident/s) once it becomes required, to ensure that it begins with "INC" or "RITM" followed by 7 digits.

Help:

def field = getFieldById(getFieldChanged())
def val = field.getValue() as String

if (!val.matches(/(?i)(INC|RITM)[0-9]{7}/)) {
field.setError("Required Format! Must be [INC#######] or [RITM#######]")
} else {
field.clearError()
}

Issue:

I placed this into a second field server-side script within the Behavior. It is working, but how do I handle the blank field value? It is failing the regex validator since its blank. I need it to only regex validate once the field becomes required.

1 answer

1 vote

I think you are very close ... try this in your second field server-side script:

def field = getFieldById(fieldChanged)
def val = field.formValue as String

if (val && !val.matches(/(?i)(INC|RITM)[0-9]{7}/)) {
   field.setError("Required Format! Must be [INC#######] or [RITM#######]")
} else {
   field.clearError()
}

I change how to get the value to use formValue instead of value. I find this more reliable.

And then in the if() we check first if the val exists. if(val) by itself will be true only if val exists and contains some text. If the first condition on the left of && fails, the rest of the condition is not evaluated and it moves the else block.

Thanks Sir Sheehan! Using your approach this did iT!

def field = getFieldById(getFieldChanged())
def val = field.getValue() as String
def regexFormat = /(?i)(INC|RITM)[0-9]{7}/
def regexFormat2 = /(?i).+/

if(!(!val?.trim()) && !(val ==~ regexFormat2)) {
field.setError("Required Format! Must be [INC#######] or [RITM#######]")
} else {
field.clearError()
}

if(!(!val?.trim()) && !(val ==~ regexFormat)) {
field.setError("Required Format! Must be [INC#######] or [RITM#######]")
} else {
field.clearError()
}

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence

👁‍🗨 Confluence Team planning dashboard_Atlympics 🌈

Hi Atlassian's, How is your journey with #Atlympics 2021 so far....excited! Me too, same excitement. Here's my typical team planning and vision dashboard which I used to share to my management and ...

46 views 1 3
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you