2 Replies Latest reply on Oct 29, 2014 5:14 AM by AndrewWard

    Data Validation of String Attribute on a Window e.g. NI Number

    AndrewWard Apprentice

      I need to be able to validate a string entered is in the correct format e.g. for National Insurance Number two characters\six digits\1 character i.e.aa999999a.

       

      This calc would perform the required validation using regex comparison on the string. Altering the regex would allow for validation of other strings.

       

      import System

      static def GetAttributeValue(Request):

      if Request._NINumber =~ /^[a-zA-Z]{2}\d{6}[a-zA-Z]$/:

      Value = "NI valid"

      else:

      Value = "NI invalid"

      return Value

       

      I am having difficulty getting the calc to run and also display back to the window that it is invalid?

       

      I had hoped to apply a Before Save calc directly to the NI Number attribute - a string with length 20 - where the intention would be add to the entered data to inform the user it was not in the correct format by using this calc with a dependency set as _NINumber

      import System

      static def GetAttributeValue(Request):

           if Request._NINumber != null:

                if Request._NINumber =~ /^[a-zA-Z]{2}\d{6}[a-zA-Z]$/:

                     Value = Request._NINumber

                else:

                     Value = Request._NINumber + " invalid format"

           return Value

       

      When running test calculation within the edit formula window it returns expected values, Unfortunately when you OK and then attempt to Save the attribute with the calc it fails with message

      Error on saving changes:

      Cannot calculate an attribute if that attribute is used in a calculation.

      I then tried to create a separate windows calc Boolean field with a dependency set as _NINumber which I would then use as a trigger to write the validation message back to a third output attribute.

      import System

      static def GetAttributeValue(Request):

           value = false

           if Request._NINumber != null:

                if Request._NINumber =~ /^[a-zA-Z]{2}\d{6}[a-zA-Z]$/:

                     Value = true

           return Value

      Again the test calculate returned expected results. However because the original trigger - NI Number - is a string attribute I can't get the calculations to fire.

       

      The only thing I can come up with is to have a separate Boolean which the user would have to turn on to fire the validation against fields already entered and construct a string advising about all the fields which may be wrong. Would prefer to do this automatically so that it doesn't get fired before the fields have been entered. I've also got to figure out a way to reset it so it can be done again after any attempted corrections.

       

      Anyone got any ideas on how best to achieve this or have a different approach to try.

        • 1. Re: Data Validation of String Attribute on a Window e.g. NI Number
          Stu McNeill Employee

          Hi Andrew,

           

          As you've seen the big issue here is that you can't use a String field to trigger a calculation to run.  This is intentional to avoid running calculations on every keystroke which could potentially cause performance issues.

           

          Unfortunately I can't think of a simple solution to this if your goal is to have the validation at runtime and block saving the record.  The other approach to this is to divert the process flow (if this is an object that uses a process anyway).

          1 of 1 people found this helpful
          • 2. Re: Data Validation of String Attribute on a Window e.g. NI Number
            AndrewWard Apprentice

            I have got this partially working by using a boolean trigger field on the window - Requestmanagement\Request\_Iconfirm - which the user can selects to confirm they have completed the data entry. This is a relationship to ordered list System\_YesorNo

            temp2.png

            This fires a calc - RequestManagementRequest\Request\_IConfirmCalc - to construct a string to feedback on any fields which have failed validation in RequestManagement\Request\_LGSSDataValidationMsg

            temp.png

            The problem is the trigger field remains set to true which I need to reset so they have to choose again after any corrections have been made.

             

            It must be noted the window is being completed in the Self Service Portal and I am totally at a loss as to how I could change the process to trap failed validations and prevent the add to cart proceeding.

            My thoughts are to only allow progress if the error string is empty but at a complete loss how this would be done.

            An alternative could be using the trigger field since it is declared as mandatory. If I could clear it when sending the errors back to the screen then add to cart would display the mandatory field not set system message forcing completion of the I confirm trigger.