Field (regression validation) based on a single selection list

Lakshmi S April 10, 2024

Hi Team,

 We have a requirement to validate the field (regression expression) based on the single selection field.

We have 2 fields, "Domain Name" and "Record Name".

If the "Domain Name" is test.com or test.net or test.us, etc,

then "Record Name" field should validate abc.test.com or abc.test.net or abc.test.us, etc


Example : If the user selects the value "test.com" from the field "Domain Name", the user should able to enter abc.test.com in the "Record Name" field.

 

1 answer

1 vote
Sean Chua _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.
April 11, 2024

Hey @Lakshmi S ,

Good to see you around again. Not sure where or how you would like to apply this, but if it is for create/issue screen - then Behaviours can set this up.

This is just a sample, so you need to match it with your instance. Something like:

import com.onresolve.jira.groovy.user.FieldBehavioursimport groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def domainNameField = getFieldById("customfield_XXXXX")
def recordNameField = getFieldById("customfield_XXXXX")

def domainName = domainNameField.getValue()
def recordName = recordNameField.getValue()

if (domainName && recordName) {
    //the regex pattern
    def expectedPattern = ".*\\.${domainName}"
    
    //validation of it
    if (!recordName.matches(expectedPattern)) {
        recordNameField.setError("Record Name must end with .${domainName}")
    } else {
        recordNameField.clearError()
    }
}

 

It should show something like this when it is wrong on create/issue screen: 

image.png
Hope that helps, do feel free to Get Help  with us from Adaptavist/ScriptRunner.

Regards,
Sean

Lakshmi S April 12, 2024

Hi @Sean Chua _Adaptavist_ ,

Thank you for always being so helpful. We use ScriptRunner a lot. :-)

We want this behaviour on both create and update of the "Domain Name" field. I am getting this error, and its not working. the ticket is creating without validating the field.


domain.png

Sean Chua _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.
April 18, 2024

Hey @Lakshmi S ,

The imports are (2 separate lines):

import com.onresolve.jira.groovy.user.FieldBehaviours

import groovy.transform.BaseScript


=====================

Do give it a go again and see if that works. if it doesn't work, do open a Support Ticket  with us to investigate. There could be other issues which may be impacting it.

Regards,
Sean

Lakshmi S April 18, 2024

Oops Sorry @Sean Chua _Adaptavist_ , I have corrected that when you gave me and separated those two in different lines, but its still same error.

groovy.png

Lakshmi S April 30, 2024

Hi Team

This is correct code. It validates the custom field value and displays the error. 

 

Note : We should add both fields with the same script (server-side script) to change the validation accordingly.

 

def domainNameField = getFieldById("customfield_39801") // Domain Name

def recordNameField = getFieldById("customfield_39803") //Record Name

def domainName = domainNameField.getValue()

def recordName = recordNameField.getValue()

if (domainName && recordName) {

    // The regex pattern

    def expectedPattern = ".*\\.${domainName}"

    // Validation

    if (!(recordName =~ expectedPattern)) {

        recordNameField.setError("Record Name did not end with .${domainName}. Please correct it.")      

    } else {

        // Clear any previous error

        recordNameField.clearError()

    }

}

Suggest an answer

Log in or Sign up to answer